Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 204
Slauma Avatar asked Apr 06 '10 12:04

Slauma


People also ask

How do I update Entity Framework model from database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do you modify EDMX if changes are made in the database?

Delete existing model and then update: Delete key to delete all models in the designer. IMPORTANT: Do not save the EDMX at this point if you are using TFS for source control!* Now right-click and select "Update Model from Database" to recreate the entire model again. Rebuild project to propagate changes.

How do I update my entity?

The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.


2 Answers

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.

like image 194
Craig Stuntz Avatar answered Nov 26 '22 05:11

Craig Stuntz


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.

like image 29
ThePG Avatar answered Nov 26 '22 05:11

ThePG