Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving "The model backing the 'Context' context has changed... on Add-Migration after EF 5 upgrade

We are building an app using code-first migrations. We had been using Entity Framework 5.0.0 RC, and we updated our NuGet package for Entity Framework. The database has been in sync with the models context. Code first migrations have been working fine for months.

When we modify our models, and try to add a migration, or even query for migrations, we receive the System.InvalidOperationException for the model backing changed.

Well, obviously it has, I'm trying to add a migration.

We're not sure what to do from here. If I remove the changes, I can run Get-Migrations successfully and it shows that the database has all the migrations applied correctly. Although the ProductVersion column says "5.0.0-rc.net45".

Has anyone else experienced this problem?

like image 942
nerickson Avatar asked Mar 02 '13 00:03

nerickson


People also ask

What is EF context?

The context class is a most important class while working with EF 6 or EF Core. It represent a session with the underlying database using which you can perform CRUD (Create, Read, Update, Delete) operations. The context class in Entity Framework is a class which derives from System.

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.

How do I enable-migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.


1 Answers

Found it. The source of this problem was code that set the DbContext.CommandTimeout in our constructor for the same class, like in this other question. Accessing that property started an initialization process that was giving us the error of what the command we actually invoked was trying to solve.

like image 67
Kevin Gorski Avatar answered Oct 14 '22 03:10

Kevin Gorski