8

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?

Sam Rueby
  • 5,914
  • 6
  • 36
  • 52
  • 1
    Your prblem isn't the Entity Framework its with changing schemas once development is done, you have to ask yourself, why do I need to continually make schema changes in the ifrst place! – Lloyd Feb 24 '12 at 23:32
  • 1
    I've been building line-of-business web apps for a decade and my experience is that schema changes and migrations are very common. – William Gross Mar 22 '12 at 18:11

3 Answers3

4

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/

KristoferA
  • 12,287
  • 1
  • 40
  • 62
1

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

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

mindandmedia
  • 6,800
  • 1
  • 24
  • 33
0

Your two requirements are in contradiction. You want to blow up whole model but in the same time you want to keep your customization. The first situation was possible in Linq-to-Sql designer but every change was lost every time you updated the model. For EF Designer MS decided to use second approach where designer almost never touch anything already existing in your CSDL spaces (entities) but changes only storage model and you must modify model little bit (in designer) manually after some breaking changes. I used both designers and have to say that productivity and usability of the second approach is much better. That is also reason why people usually don't complain about this.

Those who complain usually do either:

  • Buy some tool (like a great one referenced by @Kristofer) because MS completely skipped anything related to merging changes on different level of granularity and its not going to be better in the near future
  • Write a script or custom tool which will put all changes back every time they delete whole model (that was a way we used with Linq-to-sql)
  • Don't use designer and maintain XML manually
  • Go to code mapping instead of EDMX
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • No, you misunderstood. I'm never touching the code model. I want the model to be entirely based on the database, all the time. If something changes with the database, no matter what the change be, the Entity Framework model should be updated to be an exact match. _Never_ vice versa. – Sam Rueby Feb 26 '12 at 23:22
  • Ok, I really misunderstood part of your question but now you at least know why it happens. Designer doesn't track your changes and it doesn't remove items from CSDL to avoid deleting your possible changes. – Ladislav Mrnka Feb 27 '12 at 07:01