How do you properly update an Entity Model following a database schema change?

entity-framework entity-model


I've made some changes in the table structure and especially the relationships between tables in my SQL Server database. Now I want to update my Entity model based on this new database structure.

Right clicking on the edmx file I find the option "Update model from database". But when I do this I get kind of a 50% update: The new columns appear in the Entity classes but I am confused about a lot of navigation properties which are still there in the model although the corresponding foreign key relationships do not exist anymore in the database. (Edit: Also members in the model classes are not deleted although the columns in the database have been deleted.)

Am I doing something wrong? Or is there another option to update the model including deletion of navigation properties? Or do I have to delete those navigation properties manually in the model files?

I am using Entity Framework Version 1 (VS 2008 SP1).

Thanks for help in advance!

4/6/2010 12:38:28 PM

Accepted Answer

You're doing the right thing to update. However, after you automatically generate a model, you can customize it. When you update, the EF designer wants to try and preserve any customizations you might've made. It has to guess about this, and it's not always right. I will try and explain how it guesses, as this might help you out.

Your EDMX has three parts:

  • Client schema (CSDL)
  • Store schema (SSDL)
  • Mapping between client and store (MSL)

The designer "owns" the store schema. It will regenerate this almost from scratch every time you update. Although it is possible to customize the store schema by manually editing the SSDL, you often lose these changes when you update your model.

You, on the other hand, "own" the client schema. The designer would generate the client schema for you the first time it runs, and it will generate client schema for newly-imported database metadata objects, such as newly-mapped tables or columns. But once the client schema has been generated for an object, it will usually not be regenerated, because the designer is trying to preserve your customizations. If, therefore, you change your database in such a way as to affect the client schema, you must update the client schema manually. You can do this in the GUI designer, or manually, in the XML.

In your case, the easiest thing to do would probably be to just select the navigation in the GUI designer and delete it. This is presuming that there is no ID property for the navigation in the database anymore, on either side of the relationship.

4/6/2010 12:49:32 PM

Popular Answer

If you are working with an entity model that has not been customised and you simply need to get changes from your SQL database into your VS project, I find it easiest to delete the entity model and to regenerate it from the same source. That way all three parts of your EDMX will reflect what is in your database. I have used the "Update model from database" option before and then edited the XML/designer and found it to be tedious and time consuming work when dealing with large databases. Don't get me wrong, it works 100%, but it is just so much faster to keep your model fresh by recreating it. Even if you DO have to edit a few LINQ-to-SQL queries afterwards, I find it better editing LINQ-to-SQL queries than editing EDMX models. That said, I have not had to do that yet. IMPORTANT: Make sure you recreate your model with the same settings as before, for example, if you created it without pluralising the object names before, do the same.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow