21

I started off my project by using Entity Framework Code First. When I was ready I uploaded my database and code to my host provider. Everything worked.

I need to add a new field to one of my classes and I don't want to loose the data in the database. Thus, I tried following some blog posts about using Code First Migrations. I did the following:

  1. I backed up my remote (production) database.
  2. I attached this database locally
  3. I added the property to my class
  4. PM> Enable-Migrations
  5. PM> Add-Migration AddSortOrderToCar
  6. PM> Update-Database
  7. At this point I created a .bak file of the local database and then used that file to 'restore' to the remote one.
  8. Lastly, I published the code to the remote site.

When I visit the site I get the following error message: The model backing the 'blahblah' context has changed since the database was created. Consider using Code First Migrations to update the database.

What am I doing wrong?

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
Scott Dietrich
  • 686
  • 1
  • 8
  • 20

2 Answers2

27

From my experience that suggests that migration table is out of sync (even if your data isn't), and that's been part of the db schema now (since 4.3 I think - under system tables).

There could be many reasons and ways to experience that error , but most of the time...

The problematic part is some combination of manually backing/restoring the full database with code changes alongside - I'm not entirely certain as to why always.

In short, even if Db-s are the same migration table data might not be - and hash comparison may fail (still full restore sounds like good enough - but you have 'two sides').


What works for me is to use
Update-Database -Script

That creates a script with a 'migration difference',
which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).

If that still doesn't work - you can still do two things...

  1. Remove the migration table (target - under system tables) - as per http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx comments in there - that should fail back to previous behavior and if you're certain that your Db-s are the same - it's just going to 'trust you',

  2. As a last resort I used - make a Update-Database -Script of the full schema (e.g. by initializing an empty db which should force a 'full script'),
    find the INSERT INTO [__MigrationHistory] records,
    just run those, insert them into the database,
    and make sure that your databases - and code match,

that should make things run in sync again.

(disclaimer: this is not a bullet proof to work at all times, you may need to try a few things given your local scenarios - but should get you in sync)

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • 1
    'Update-Database -Script' worked. I would have thought that the 'Update-Database' command would have done the same thing (but without generating a script). Anyway. Thanks! – Scott Dietrich Apr 22 '12 at 17:44
  • 1
    @ScottDietrich - np - well, it should, but you have to keep things in sync on all sides (and e.g. migrations generate based on the 'status' of the local db etc. - so migration is made to fit your 'local' db vs code - once you start 'moving things out' there is more chance you did something wrong). All in all, SQL scripts turned out to be safer way, you can see exactly - unless you have a dev setup on the server too (to be able to run migrations there 'in place') – NSGaga-mostly-inactive Apr 22 '12 at 17:51
  • I had a DB where some migrations had run but the records in the __MigrationHistory table didn't stop those same migrations from trying to run again. I ended up deleting all rows in the __MigrationHistory table, and then inserting the INSERT INTO [__MigrationHistory] lines generated by the -Script command manually. The migration history is now in order again. Thanks for a very helpful answer @NSGaga! – Tormod Haugene Jan 07 '15 at 11:32
1

I think in step 6 you need to run Update-Database -Verbose

Also this link is very helpful for updating database in EF with Scaffolding http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-entity-framework-scaffolding-and-migrations

srinced
  • 124
  • 8