model backing the context has changed since the database was created. Consider using Code First Migrations to update the database

14,401

Solution 1

Database.SetInitializer<DbContext>(null);

http://patrickdesjardins.com/blog/the-model-backing-the-context-has-changed-since-the-database-was-created-ef4-3

Solution 2

I got a similar problem :

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

I have one project for MVC and another project for the model, context and repositories. I've been working on it for weeks but today it said stop.

I have tried to delete database, enable-migration, add-migration and update-database so many times that I've lost count. I've added initializers to MigrateDatabaseToLatestVersion as well as DropCreateDatabaseIfModelChanges.

What finally made it work was to move model, context and repositories into the MVC project (not something I was keen on)...then it worked right out of the box without any code changes at all (besides namespaces)! Very strange...

I've read so many blog posts during the day trying to solve this problem. One of them (I don't know which one) mentioned a bug in Visual Studio 2013 where reference to DLL files weren't always updated as they should, suggesting that my MVC project missed out something when I was running add-migration and update-database in my separate project. But it's just a guess.

I'm using EF 6.1 and .Net 4.5.1 in my solution.

Solution 3

Just to elaborate on RouR's answer:

I had an MVC web project that had a model attached.

I then created a console app that consumes this model.

My console app, only is aware of the connection string and has EF reference, just doesn't know how to let each one communicate.

Hence, the model backing database changed error appears

The engine is just confused how to proceed it sees and expects an entity, as was reference, just losses it when it fails to find the proper context, and assumes context wan't migrated properly.

So here we are,

Database.SetInitializer<MyProject.Models.MyModel>(null);
Share:
14,401
Shiloh
Author by

Shiloh

Updated on August 01, 2022

Comments

  • Shiloh
    Shiloh over 1 year

    I am using MVC5 and EF6 CodeFirst migrations. My model is in a separate project from my MVC web app and I keep getting this error when trying to access the model classes. I have automatic migrations already enabled. I can drop the entire database and then using update-database to regenerate everything I still get this error. The error is wrong because the context has not changed since I created the database. Also, through a Unit Test project, using the same calling code as I have in my MVC app, I can reference the same Model project, access the model classes and data. I have the Model separate from the MVC project because I need to be able to reuse the Model outside of the web.

    The model backing the "xx" context has changed since the database was created. Consider using Code First Migrations to update the database

  • CB4
    CB4 over 7 years
    Someone please explain to me what is the disadavantage of assigning the Database.SetInitializer(null); I have tried this and it seems to work for me. However, I am not sure what to expect in the future if I need to modify my db table. Will the code first migration no longer work or what. Thanks
  • TheAlbear
    TheAlbear over 4 years
    FYI: This needs to be added to the protected void Application_Start() of the MVC project