How to manually update your Entity Framework model classes when they get out of sync with your database

The ADO.NET Entity Framework is an awesome way to quickly pull data into an app. I’ve been building apps for a really long time and have used all the frameworks that have come out over the years. IMO EF is the best one I’ve used yet.

Well, I should say it is awesome MOST of the time. The biggest beef I have with it is that the models can get out of sync with the database and saving the EDMX file and running the “Update Model from Database…” feature doesn’t work.

(The Update Model from Database option doesn’t update the object model and class files)

For example, let’s say I start with this model:

And I add a new table to the DB called Foo:

I then go back to my app and add my new table to the model using the “Update Model from Database…” feature:

And it is added to the diagram:

But the corresponding object model and CS file wasn’t generated:

Up until today I would have recreated the EDMX file, which is a big pain. But I discovered that there’s a context menu item called “Run Custom Tool”.

The most obvious choice is to select “Run Custom Tool” from the EDMX file, but that doesn’t do anything:

If you F4 the corresponding T4 file you can see that the custom tool associated with the T4 file is: TextTemplatingFileGenerator

So, I selected “Run Custom Tool” from the T4 context menu:

And that generated the Foo.cs file:

But I still didn’t have the Foo class in my object model:

So I selected the “Run Custom Tool” on the tt file:

And that generated the correct object model:

So anytime you find your EF model and/or class files out of sync with your database then you need to manually run the “Run Custom Tool” command on both the [Entities].tt file and the [Entities].Context.tt file.

This is a big pain. I will ping the product team to see if they can run the custom tool on the EDMX file when it is saved and recursively run on all the tt files associated with that EDMX parent.

Jon