Entity Framework Code Only error: the model backing the context has changed since the database was created

I created a "Code Only" POCO for use against an existing database using Entity Framework 4 and the CTP4. When I run a query I get the error

The model backing the 'xyzContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

I'm unclear on why this is happening or what I can change. I merely created the POCO, defined a simple DbContext, made a few tweaks, and then tried to run a simple query. Since I'm using "Code Only", I'm unaware of any configuration settings that need to be made. And I certainly don't want to recreate or delete the database since it's an existing database.

Thanks for any ideas.


I found the answer in the comments on this post on Scott Guthrie's blog.

http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx

For those who are seeing this exception:

"The model backing the 'Production' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance."

Here is what is going on and what to do about it:

When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:

Database.SetInitializer<Production>(null);

This is a bug in CTP4 for using EF with pre-existing databases.

You can fix it by calling:

Database.SetInitializer<YourContext>(null);

in the Application_Start method of Global.asax


I commented above and it worked at the time when I just playing around with EF5 to familarise myself with it's workings. Now I'm writing "actual" code and I've moved away from setting a database initialiser per context in code because of a architecture I've settled on using MEF to instantiate any DbContext and inject all configuration dependencies as composable parts.

So again I immediately ran into the error described above but this time I choose to resolve it using configuration file entries as below.

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <contexts>
      <context type="Basd.Erp.ContactContext, Basd.Erp" disableDatabaseInitialization="true"></context>
    </contexts>
  </entityFramework>

So by setting disableDatabaseInitialization="true" in the config file section for entityFramework you can overcome the error described above and since it's not in code one of the benefits is the ability to "more easily" use abstracted builders/factories to create context.