Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change migration file manually and keep compatible __migrationhistory record

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?

like image 992
Matheus Moreira Avatar asked Nov 13 '12 18:11

Matheus Moreira


People also ask

What is __ Migrationhistory?

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.

How do I update an existing database using migration?

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.


2 Answers

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/

like image 29
vbigham Avatar answered Oct 16 '22 02:10

vbigham


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

[ForeignKey("FK_RecebimentoMaterial_CompFab_ComponenteFabricante_Id")]
YourNavigationProperty
like image 174
arni Avatar answered Oct 16 '22 00:10

arni