Donald Hughes
Donald Hughes

Reputation: 6927

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.

Upvotes: 51

Views: 47624

Answers (7)

Jose Ortiz
Jose Ortiz

Reputation: 3

For this Error in my case, i just deleted all records on "_MigrationHistory" Table, from "DBControlContext". I hope this is helpful.

Upvotes: 0

Justin
Justin

Reputation: 1503

This was resolved for me by adding this to by context constructor.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

After doing this my code-first migrations now run automatically when the db already exists.

Upvotes: 1

Batman
Batman

Reputation: 702

I had the same issue - re-adding the migration and updating the database didn't work and none of the answers above seemed right. Then inspiration hit me - I'm using multiple tiers (one web, one data, and one business). The web layer never threw this exception - it was the business layer (which I set as console application for testing and debugging). Turns out the business layer wasn't using the right connection string to get the db and make the context. So I added the connection string to the app config and viola it works. Putting this here for others who may encounter the same issue.

Upvotes: 1

Jared Beach
Jared Beach

Reputation: 3133

All I had to do to was drop the __MigrationHistory table.

Context:

I received this error when I changed the name of a table. After I added the annotation [Table("NewTableName")] to one of my models, Entity Framework generated a __MigrationHistory table.

Upvotes: 5

rism
rism

Reputation: 12132

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.

Upvotes: 13

Steve Lydford
Steve Lydford

Reputation: 428

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

Upvotes: 32

Donald Hughes
Donald Hughes

Reputation: 6927

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);

Upvotes: 80

Related Questions