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?
What is Migrations History Table? Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database. By default the name of the table in the database is __MigrationHistory and it is created when applying the first migration to the database.
Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.
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/
Have you tried specifying the foreign key name manually in your model, using the ForeignKey attribute?
[ForeignKey("FK_RecebimentoMaterial_CompFab_ComponenteFabricante_Id")]
YourNavigationProperty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With