6

My team and I are having a lot of problems trying to keep our development (and, soon, the client's production) database in sync with our model while using Entity Framework Code First Migrations.

It seems the problems started to happend when I had to manually edit a migration code because of a faulty DropForeignKey() method call. (My project is using MySQL 5.5, MySQL Connector 6.6.2, EF 4.3.) The problematic command:

DropForeignKey("Recebimento", "ComponenteFabricante_Id", "CompFab");

was changed to:

DropForeignKey("Recebimento", "FK_RecebimentoMaterial_CompFab_ComponenteFabricante_Id");

Since then, every time I try to Update-Databse, even when I know that I already have all migration code that reflect the current model mappings, the following message is displayed: "Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration."

I don't want to use change AutomaticMigrationEnabled to true, so I run Add-Migration Test to see what is been created. The file created contains commands that were already applied (by previous migrations) to the database. If I try and run this last migration, Update-Database fails (something like "Column already exists" or "Table already exists", which are the correct error messages since the migration has duplicated code).

I suspect the problem has something to do with the values the table __migrationhistory saves on the Model column (a binary representation of the model applied by the migration) - maybe the value is not the correct value since a manual correction was made.

What can I do to be able to manually edit migration code and have it work properly? What are the best practice when using Entity Framework Code First migrations?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matheus Moreira
  • 2,338
  • 4
  • 24
  • 31
  • Did you ever find a solution to this? It's not the __migrationhistory value that's the problem, it's the resx files that AddMigration creates. They hold a snapshot of the database, therefore it seems you can't change (or add) a file manually. – dwhite Jul 10 '13 at 11:24
  • I have a slightly similar problem, look at my question in SO, it may help: http://stackoverflow.com/q/11806570/1135871 – Vahid Hassani Jun 30 '14 at 09:10

2 Answers2

1

Have you tried specifying the foreign key name manually in your model, using the ForeignKey attribute?

[ForeignKey("FK_RecebimentoMaterial_CompFab_ComponenteFabricante_Id")]
YourNavigationProperty
arni
  • 2,152
  • 19
  • 14
1

I came across a similar problem where my __migrationhistory table became out of sync with the model. I had recreated an existing database using code-first, but at that time my team did not have the time to learn migrations and get them working. We were managing the model changes by keeping track of SQL statements. So, when I finally attempted to start using migrations, there were changes that had been applied everywhere, but EF wanted to create migrations.

To solve this in my case, I was able to drop the __migrationhistory table, and create a new initial migration that did not make any changes to the model. This may have been unnecessary since you can add-migration and pass it a parameter to -IgnoreChanges. This should update the __migrationhistory without actually requiring any database SQL changes to run.

Anyway, I wanted to post here because I my problem was similar and I ended up looking at this thread hoping for a solution. I hope someone else will find some use from this article by LADISLAV MRNKA, as I wished I had seen it earlier.

http://www.ladislavmrnka.com/2012/03/ef-4-3-migrations-and-existing-database/

vbigham
  • 191
  • 1
  • 6
  • 1
    Yep. I messed up my migrations and started again. Now I follow the tips here http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/ especially the "tear all the way down and up again" bit – Colin Oct 17 '13 at 18:44
  • Thanks for that link. It should really come in handy too. – vbigham Oct 29 '13 at 22:58