Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model backing a DB Context has changed; Consider Code First Migrations

The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

What causes this to happen? I've literally just created a brand new database and have changed nothing, but every time I try to access a model from a controller it throws this.

Edit

It has something to do with the fact that I was attempting to share a connection string (i.e. a database) with two separate entities.

like image 204
Brian D Avatar asked Feb 19 '13 01:02

Brian D


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 are Code First migrations?

Code First Migrations allow you to create a new database or update an existing database based on your model classes using the Package Manager Console for running commands. If you are using the EF code first approach then there are more ways to initialize the database provided by the Entity Framework as follows.

How do I enable Code First migrations?

Go to Package Manager Console and type command help migration. Type Enable-Migrations -ContextTypeName EXPShopContext. This command creates a migration folder with InitialCreate. cs and Configuration.


1 Answers

In my case this error was caused by the existence of the _MigrationsHistory table in the database. Deleting that table fixed the problem. Not sure how that table got into our test environment database.

like image 114
Craig Fisher Avatar answered Oct 12 '22 11:10

Craig Fisher