The model backing the \'MyDbContext\' context has changed since the database was created. Consider using Code First Migrations to update the database (http://
To solve this error write the the following code in Application_Start() Method in Global.asax.cs file
Database.SetInitializer<MyDbContext>(null);
If you have changed the model and database with tables that already exist, and you receive the error "Model backing a DB Context has changed; Consider Code First Migrations" you should:
pm>update-database -Verbose
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.
This error occurs when you have database is not in sync with your model and vice versa. To overcome this , follow the below steps -
a) Add a migration file using add-migration <{Migration File Name}> through the nuget package manager console. This migration file will have the script to sync anything not in sync between Db and code.
b) Update the database using update-database command. This will update the database with the latest changes in your model.
If this does not help, try these steps after adding the line of code in the Application_Start method of Global.asax.cs file -
Database.SetInitializer<VidlyDbContext>(new DropCreateDatabaseIfModelChanges<VidlyDbContext>());
Reference - http://robertgreiner.com/2012/05/unable-to-update-database-to-match-the-current-model-pending-changes/
Adding this as another possible solution, because this is what fixed it in our case;
Make sure if you have multiple projects that they are using the same Entity Framework Nuget package version!.
In our case we had one project ( call if project A ) holding the EF code first context with all entities. It was this project that we were using to add migrations & update the database. However a second project ( B ) was referencing project A to make use of the context. When running this project we got the same error;
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).
Entity Framework detects something about the model has changed, you need to do something to the database to get this work. Solution: 1. enable-migrations 2. update-database