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

entity-framework entity-model


In my SQL Server database, I've made certain modifications to the table structure, particularly the connections between tables. Now that the database schema has changed, I want to update my entity model to reflect it.

I can choose the "Update model from database" option by selecting "right click" on the edmx file. However, I only receive around a 50% update when I do this: The Entity classes now have the new columns, but I'm perplexed by the many navigation attributes that are still there in the model even if the relevant foreign key connections are no longer present in the database. (Edit: Although the columns in the database have been destroyed, members in the model classes are not deleted either.)

Am I doing incorrectly? Or does updating the model also allow for the removal of navigation properties? Alternatively, do I need to manually remove those navigation properties from the model files?

Entity Framework Version 1 is what I'm utilizing (VS 2008 SP1).

I appreciate the assistance in advance.

4/6/2010 12:38:28 PM

Accepted Answer

Updating is the proper course of action. However, you may alter a model that was generated automatically. The EF designer wants to attempt to save any adjustments you may have made when you upgrade. It must make an estimate about this, and it often makes mistakes. I'll attempt to describe how it makes assumptions in the hopes that it will be useful to you.

Three components make up your EDMX:

  • Customer schema (CSDL)
  • store layout (SSDL)
  • mapping between customers and retailers (MSL)

The shop schema is "owned" by the designer. Every time you upgrade, it will essentially recreate this from scratch. Although manually changing the SSDL allows you to modify the store structure, you often lose these changes when you update your model.

The client schema, on the other hand, is "owned" by you. When the designer initially starts up, it will automatically create the client schema for any newly imported database metadata objects, such as newly mapped tables or columns. However, once the client schema for an object has been built, it normally won't be generated again since the designer wants to save your adjustments. Therefore, you must manually update the client schema whenever you make changes to your database that have an impact on it. You may accomplish this manually in the XML or using the GUI designer.

The simplest course of action in your situation would likely be to just pick the navigation in the GUI designer and eliminate it. This assumes that neither side of the connection has an ID attribute for navigation in the database anymore.

4/6/2010 12:49:32 PM

Popular Answer

I find it best to remove the entity model and regenerate it from the same source when dealing with an entity model that has not been customized and you merely need to receive updates from your SQL database into your Visual Studio project. In this manner, the data in your database will be reflected in all three components of your EDMX. When working with huge datasets, I had previously utilized the "Update model from database" option and then modified the XML/designer and found it to be laborious and time-consuming work. Without a doubt, it works well, however building a new version of your model is considerably quicker. I believe it is preferable to change LINQ-to-SQL queries rather than EDMX models, even if you DO need to edit a few LINQ-to-SQL queries thereafter. Having said that, I've never had to do it. Important: Be careful to replicate your model using the same parameters as the original. For instance, if you didn't previously pluralize the object names when you built it, keep doing so.

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