Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 515
Shiloh Avatar asked Feb 23 '14 19:02

Shiloh


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

What is the purpose of code first migrations?

Code First Migrations is the recommended way to evolve your application's database schema if you are using the Code First workflow. Migrations provide a set of tools that allow: Create an initial database that works with your EF model. Generating migrations to keep track of changes you make to your EF model.

Which are the to update migration command for update the database in code first approach?

We have three commands => Enable Migrations, Add Migration, Update Database. Now, go to Shop. cs Class in Model Folder and add new property. Go to Package Manager Console and type command help migration.


2 Answers

Database.SetInitializer<DbContext>(null);

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

like image 136
RouR Avatar answered Jan 04 '23 17:01

RouR


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.

like image 36
Gkrish Avatar answered Jan 04 '23 17:01

Gkrish