Wade73
Wade73

Reputation: 4519

Getting EF Context has Changed Error After Updating Site

I am having an issue where I am getting an error "context has changed since the database was created," but when I look at the migrations history table it is the latest migration. This is a test site, so I updated the database using a sql script I got by running this command "Update-Database -Script -SourceMigration: $InitialDatabase" I can't just delete the whole database and recreate it. Has anyone run into a similar issue like this? Currently, I am using EF 6.1.3.

Note: I used code-first for this, so it was not an existing database I am adding to.

Upvotes: 0

Views: 91

Answers (3)

Wade73
Wade73

Reputation: 4519

The issue was that a new directory had been created for the site due to the domain name being changed. I knew the domain name had changed, but didn't realize the physical directory had changes. It was such a slight change I missed it when I was checking the application paths (an s was removed). So the problem was caused by pebkac (problem exists between keyboard and chair).

Upvotes: 1

Peter
Peter

Reputation: 1612

Have you tried using this command instead of executing a script?

Update-Database -Force

If you have enabled auto migrations, this should pick up your code-first changes and deploy them. The force will allow columns which have data to be dropped etc.

Upvotes: 0

A.Ima
A.Ima

Reputation: 266

Try to set your Initializer to null in the constructor of your DbContext class.

Like this:

Database.SetInitializer<YourDbContext>(null);

ScottGu's Blog explains why this happens :

http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database

Upvotes: 0

Related Questions