If you try and update an Entity Framework model from an earlier version of EF (say EF 4.3) in Visual Studio 2013 or 2015, you will find that Visual Studio will create a couple of T4 (
.tt) files and your solution will no longer compile.
Of course, in an ideal world you could update to the latest version of Entity Framework (or even migrate away from EF all together), but for legacy software that is not always possible.
However, updating a model from an earlier version of Entity Framework is possible using a modern version of Visual Studio - either VS2013 or VS2015 is fine (the screenshots use 2015). The errors are due to Visual Studio switching to using the Text Template Transformation Toolkit (T4) method of code generation for the model. Although it is not possible to stop Visual Studio from switching the code generation strategy, it is possible to switch it back.
This walk-through will show you how.
Updating The Model
We begin by updating the model - in this case
MyEntityFrameworkModel in the traditional way. We open the
MyEntityFrameworkModel.edmx file and right-click on the design surface and select
Update Model from Database....
Follow through the wizard selecting the database connection, the version of EF that you wish to use (if the version you wish to use is not there, you can install it via NuGet), and anything from the database that you wish to add to the model, then click
Visual Studio will then busy itself for a while, before presenting a series of security warning dialogs.
The security warnings are caused by Visual Studio having switched to using T4 code generation and is running the
.tt templates. Click
OK on each- we’ll be removing them shortly.
Removing The T4
Visual Studio has generated two
.tt files - if your model is
MyEntityFrameworkModel then the files will be called
Both files need to be deleted - you can Ctrl+click on the two files then right-click and select
Delete from the context menu.
Changing Model Generation
Finally we need to change the model generation back to the old way - now called
Legacy ObjectContext. To do this, first right-click on the model design surface, and select
Properties from the context menu.
Then from the Properties tab find the
Code Generation Strategy entry and change from
Finally, build the solution (F6) and your updated model will be available.
When Microsoft decided to automatically switch Entity Framework code generation to the T4 method even for old versions of EF it made it difficult to update models in legacy code that used early versions of EF.
It is still possible to switch the code generation strategy back, allowing these models to be updated. The method is rather manual, and so only suitable for infrequent updates. If you need to update your model, then you should consider updating to use a later version of Entity Framework and the newer T4 code generation strategy.
However, for legacy code whose model is not often updated, the method shown here is a perfectly acceptable solution.