How to update correctly an Entity Model after changes of database structure?

Slauma picture Slauma · Apr 6, 2010 · Viewed 16.8k times · Source

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!

Answer

Craig Stuntz picture Craig Stuntz · Apr 6, 2010

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.