Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It's seemingly impossible to entirely replace Entity Framework data model from database

Basically, I have a Sql Server database whose schema changes. When this happens, I have to update the EF data model. This is fine if all I did was add or delete a table: go to the designer, find "Update Model From Database..." from one of the multiple locations it exists and go through the wizard.

Unfortunately, the wizard doesn't just let me replace the whole model from the database. It can also only do one thing at a time. So if I make the unfortunate decision of making multiple schema changes and even worse, forget what I did: I have to do multiple steps of adding, refreshing, and deleting tables from the model.

This is obviously cumbersome. So for a lack of a better procedure, I have to blow away the model and go through all the steps of recreating it from the database. Crap: I left the connection string in the configuration file. Now I have to go delete that and start the wizard over or else it won't generate the same entities class name and now all of my code will break.

Why can't this just blow away the model for me and generate from the database? More importantly, why hasn't anyone else asked this question? What are people doing?

like image 685
Sam Rueby Avatar asked Feb 24 '12 23:02

Sam Rueby


2 Answers

If you want to blow away and replace the model, the easiest built-in way is to just delete it and re-create it. However, if you have made any customization of the model you will of course lose that too.

As a workaround, to allow continuous incremental schema changes (and changes applied on both ends) I wrote a utility that compares the database to the SSDL layer of the EF model, and the SSDL to the CSDL layer, display the diffs and allow individual (or all) differences to be synced across.

enter image description here

You can see it in action here: http://www.youtube.com/watch?v=doqYOlcEAZM ...and if you want to try it out you can download it from http://huagati.com/edmxtools/

like image 127
KristoferA Avatar answered Sep 23 '22 06:09

KristoferA


You could write a cmd-script that does it via command-line:

http://msdn.microsoft.com/en-us/library/bb896270.aspx

like image 30
mindandmedia Avatar answered Sep 22 '22 06:09

mindandmedia