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?