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

ef-code-first entity-framework


First, I'm new to EF programming. I tried the EF 4.3.1 code first on a production database I already had, and everything worked. My database structure was just upgraded, and I now have 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).

Unable to utilizeDropCreateDatabaseIfModelChanges What is the easiest course of action to follow considering the update to the schema is already in production?

I'm grateful.

6/11/2012 2:19:07 PM

Popular Answer

Since EF-CF migrations are a relatively new idea, I advise adapting a tried-and-true procedure to work with our new technologies, like EF. What we do is this:

  1. Use DropCreateDatabaseIfModelChanges to promote regional growth. You may do this to keep your local development copy up to date with your Model (in code). Every time you build or execute, your local database is updated. An Initializer may be used to load test data, among other things.Database.SetInitializer<DBContextNameHere>(new DBContextInitializerNameHere());

  2. When comparing local development and production, use RedGate's SQLCompare tool to instantly create a change script for deployment. (Note: The utility also supports auto-deployment.)


The main advantage is that you receive a repeatable and versioned deployment through the created script AND you don't have to modify your local development process. Additionally, you may use their SQL Source Control tool to maintain source control for all of your SQL objects, deployment scripts, and even data.

I LOVE their product and how it helped me with this same situation, however I DO NOT WORK FOR THESE PEOPLE.

6/12/2012 5:13:29 PM

Related Questions


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