Shiloh
Shiloh

Reputation: 1878

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

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

Upvotes: 6

Views: 15213

Answers (5)

mca
mca

Reputation: 124

We use EF code migrations to keep databases schemas up to date. The other day I had the same problem, in my case I was working with 2 database instances (QA and DEV databases of the same client with exact same data however QA environment was throwing this error. I've fixed the problem by deleting the _MigrationHistory folder and problem solved.

Upvotes: 0

Pinch
Pinch

Reputation: 4207

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

Upvotes: 1

user3223799
user3223799

Reputation: 1

We are having the same issue. What solved it for me was not necessarily to move the Model, Context into the MVC project, but move my seperate project to the same solution as the MVC project and reference it by project folder.

This worked, but referencing a DLL instead of a project did not work.

Funnily enough, when creating a new Console Application and referencing the DLL assembly, we did not get this issue... only from an MVC project

Upvotes: 0

Gkrish
Gkrish

Reputation: 1881

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.

Upvotes: 4

Related Questions