0

I am using entity framework code first migrations. Very first time I do not have migrations enabled. When I run the project it creates _migrationhistory table with one row in it.

Then I delete this table and ran application, it ran successfully. Now I add one more property to entity and try to run but it did not run complaining that model is not compatible with database.

My question is how EF and database knows model is changed or database is different than model without _migrationhistory table or migrations in code?

Rajaram Shelar
  • 7,537
  • 24
  • 66
  • 107
  • EF only looks at _migrationhistory when it goes to apply the migration(s). If you use the migratedatabasetolatestversion, it will build a model and compare it to the model stored in a resource file of the last code migration (or a blank DB if there is none). You get that error if they don't match. See [here](https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood). – Steve Greene Aug 25 '17 at 16:41

1 Answers1

1

Entity framework first checks if the database has _migration history table. If it doesn't have one, it tries to create one and run all the migrations from the beginning and also inserts the migrations name as a record in the migration history table.

Because, you have deleted the migration history table, entity framework cannot compare its migration records with the migration files. Therefore, it tries to run all the migrations again. But, this database already has the table for the relevant entities. Therefore, an error message (model is not compatible with the database) is displayed to the user.

Tejus
  • 694
  • 1
  • 7
  • 19
  • your statement " Therefore, it tries to run all the migrations again", I do not have any migrations. – Rajaram Shelar Jul 25 '17 at 04:11
  • 1
    "dbo._MigrationHistory" table doesn't get created without any initial migrations. Check for any migration file under Migrations folder. – Tejus Jul 25 '17 at 04:30
  • 2
    Also in Configuration.cs check if AutomaticMigrationsEnabled is set to true or false. If it is set to true, entity framework automatically tries to upgrade the database without the migration files. – Tejus Jul 25 '17 at 04:32