Wellington Zanelli
Wellington Zanelli

Reputation: 1964

Entity Framework Migrations "context has changed" error persists even after reset all migrations

I have a solution with two projects, the main has the user classes (the default of MVC5) and the other one I have my business classes.

After a few changes in my code I've got the error:

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

I've executed the commands for add a new migration and for update the database. The error still goes on.

After this I tried many things to solve the problem, including these solutions:

https://stackoverflow.com/a/22451879/2394172

and

https://stackoverflow.com/a/11679386/2394172

But I still have this error:

enter image description here

PS: I already deleted my database and recreate it and the problem still on.

I can't figure out why this error still happening. When I execute the command Add-Migration the file created has the functions blank.

UPDATE (12/11/2014):

I've made a video showing all the process that I'm trying, proving that I'm using the same database I was updating with migrations.

http://youtu.be/hNVG10NynZU

By the way, I notice that I can insert data in the database, as shown in the video, but after it, the error occurs.

Upvotes: 2

Views: 1263

Answers (3)

Nikatlas
Nikatlas

Reputation: 194

Not sure but Try using -projectName when you enable migrations or you addmigrations. I remember having some problems when running migrations with multiple projects in solution. I remeber reading somewhere that they use the Set as StartUp project or somethink like that.

Upvotes: 1

Jason Hischier
Jason Hischier

Reputation: 130

Your code is failing on an inner call to CreateDatabaseIfNotExists...InitializeDatabase - for some reason, your application thinks the database doesn't exist and is trying to create it from your model code. You are using the same database for the ASPNet Identity tables and also your proprietary tables; the first time you try to access data from your proprietary table (Cms), you get the exception.

Perhaps use a different database name for the ASPNet tables so they're not in the CMS database.

Or, since you are using migrations in the Package Manager Console to create/update your schema, don't use the CodeFirst Database Initializer functionality. In your EfDbContext's Constructor, pass in null to the Database.SetInitializer<>() method to prevent attempts to initialize the database upon first access. See this link for code sample:

http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx.

Upvotes: 1

Martijn
Martijn

Reputation: 12092

If you care nothing for your existing data, the following is sure to work:

  1. Throw away your database
  2. Throw away all the migration files
  3. Run add-migration
  4. Run update-database

It's a bit of a steamroller, but it will get you out of trouble if you're not interested in finding out what the trouble actually is.

Upvotes: 1

Related Questions