What is the simplest thing to do when database schema changed using EF code first

ef-code-first entity-framework

Question

I'm new to EF code first. I have an existing database in production and I used EF 4.3.1 code first and everything worked. Now I just updated my database schema and got the exception

System.InvalidOperationException: The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

I can't use DropCreateDatabaseIfModelChanges since it is in production, what's the simplest way to take to cope with the schema change?

Thank you.

1
6
6/11/2012 2:19:07 PM

Popular Answer

Since EF-CF migrations are a fairly new concept I would suggest taking an age-old proven process and modifying it to work with our new tools, like EF. Here's what we do:

  1. Use DropCreateDatabaseIfModelChanges for local development. This will allow you to keep your local dev copy in sync with you Model (in code). Each time you build/run you get an updated local database. You can also use an Initializer to load test data, etc. Database.SetInitializer<DBContextNameHere>(new DBContextInitializerNameHere());

  2. Use RedGate's SQLCompare tool to compare local dev to production and automatically generate a change script for deployment. (Note: you can also auto-deploy from the tool)

http://www.red-gate.com/products/sql-development/sql-compare/index-b

The key benefit is you don't have to change your local dev process AND you get a repeatable and versioned deployment via the generated script. You can also combine this with their SQL Source Control tool to keep all of your SQL objects and deployment scripts (even data) in source control.

NO, I DO NOT work for these guys I just love their tool and how it helped me with this very same problem.

6
6/12/2012 5:13:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow