Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice when the database schema changes in entity framework?

I am using Entity Framework to do the ORM in .NET project. The problem I am facing is that when the database schema changes, there is no proper mechanism to update the edmx file.

For example, If there is a column called "Salary", and I change it to "EmpSalary", then when I update the edmx from visual studio and it shows me two columns in the class - Salary and EmpSalary.

One way I figured out is to delete the entire edmx file and regenerate it. But then what if I have manually renamed the navigation properties in the model? I will lose them and I have to recreate them which is a painful thing to do every time.

Any best practices in this area?

Thanks

like image 304
Jey Geethan Avatar asked Dec 14 '09 11:12

Jey Geethan


2 Answers

The approach I used with Linq To SQL (which has the same issue) is to script my manual changes to the xml file so that I could reapply them after re-running generation process. You can generate a class library for editing the edmx file using Linq To Xsd. See http://www.adverseconditionals.com/2008/05/scripting-changes-to-linq-to-sql-dbml.html for a bit more detail

like image 95
mcintyre321 Avatar answered Oct 13 '22 07:10

mcintyre321


In the EF1 designer, the "Update Model From Database" function is broken for a lot of situations. I've been working with EF1 for about a year now. Problems using the EF1 designer have cost me days of time so as far as I'm concerned the best practice is to manually edit the EDMX XML yourself.

For tricky stuff, create a new model containing the new tables / columns / relationships / views you want and then copy-paste the XML from the new EDMX into your existing XML.

Some things it breaks when you use "Update Model From Database"

  1. Overwrites any manual changes you make to the XML.
  2. Can't cope with default values in the database - insists your entities provide values for fields that are populated by default-value.
  3. Generates ludicrous compound keys for views and even if you've fixed the view to have sensible keys, it comments out the your correct version and creates a new version using its own weird keys!
  4. Doesn't set all the attributes for columns added to tables that it sets when you create the table from scratch (eg Unicode, MaxLength, FixedLength)

I'm sure there are more, but this was enough for me to stop using it.

like image 2
Dave R Avatar answered Oct 13 '22 08:10

Dave R