Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2012 - EF 5.0 - Update Model from Database - Not picking up table changes

In Visual Studio 2010 EF 4, I could add a new column to a database table, then click 'Update Model from Database' on my .edmx and all was good in the world.

In Visual Studio 2012 EF 5, I alter a table, and then click 'Update Model from Database' and it does not pick up the changes to individual tables anymore.

Am I missing something ... or is this a new 'feature' of VS2012?

Update The only way I can get a table with an added column to reflect in the .EDMX file is to delete the table then 'Update Model from Database' and it will pick up the added column. However, it puts the added table far off to the left and I have to move it back in place. No biggie just an annoyance.

like image 715
c0d3p03t Avatar asked Oct 24 '12 17:10

c0d3p03t


People also ask

How do you update an EDMX file with database changes?

Update the .edmx file when the Database changes In the Model Browser, right-click the . edmx file and select Update Model from Database. Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the . edmx file.

Which option in EDMX can be used to get the latest changes from the database?

Use the update model wizard (to update the storage model), open the . edmx file using the XML editor, find the desired property in the CSDL (conceptual model) section and change the desired attributes. This is basically the same as option 1, but you're editing the XML directly (a find and replace might be useful here).

How do I update table mapping in Entity Framework?

The easiest way to resolve this issue is to right click on the Entity Model and choose "Update Model From Database". Then select the "Refresh" tab and find and select only the table you wish to map a column for. Click the Finish button and you should have everything properly mapped.


2 Answers

Yes, this still works in VS2012. Are you using the default code generation or do you have a TT file? It could be that it's being added to the EDMX but your class files are not being updated.

Try right-clicking on the EDMX file or the relevant TT file in Solution Explorer and choose "Run Custom Tool" to make sure it's being regenerated.

like image 190
Tobias J Avatar answered Sep 28 '22 10:09

Tobias J


There is a difference between having an EF Model and how it is underlined in your database. When you "Update model from database", if the entity/table is already imported into your data model then you will be refreshing the entity. This process will not automatically map your database schema to your existing data model, you can however add a property to the entity in your EF model, and then map the property to the database column (this is exactly what EF does for you automatically when you delete and update normally).

When you delete the entity first, then update EF knows that the table/entity has not been imported before and so will map all of the database columns automatically for you.

In summary, you don't HAVE to delete the entity, but it can be easier to do so for simple entities.

like image 44
Ryan Amies Avatar answered Sep 28 '22 10:09

Ryan Amies