简体   繁体   中英

Error re-scaffolding multiple EF Code First migrations merged from branch to trunk

I am using EF 6.0.1 in VS 2010 and having a problem where I am merging multiple migrations from a branch into trunk after a migration in trunk has been applied to the production database and I cannot re-scaffold the former migrations without errors. In my situation, there are about 50 migrations being merged - the example below has just two to illustrate the problem.

Note that I could recreate the migrations in trunk, but I would rather not as we have about 50 migrations and many are heavily edited and also have plenty of raw SQL doing data manipulation as well. This would be a big job to recreate, not to mention it would result in just a single massive migration.

Here is a simplified scenario:

  1. I have a domain entity class Widget:

     public class Widget { public int Id { get; set; } } 
  2. Trunk is branched to BigProjectBranch

  3. In BigProjectBranch, I add a Name property to Widget and create Migration1:

     public class Widget { public int Id { get; set; } public string Name { get; set; } } public partial class Migration1 : DbMigration { public override void Up() { AddColumn("dbo.Widgets", "Name", c => c.String()); } //... } 
  4. Still in BigProjectBranch, I add a Category property to Widget and create Migration2:

     public class Widget { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } } public partial class Migration2 : DbMigration { public override void Up() { AddColumn("dbo.Widgets", "Category", c => c.String()); } //... } 
  5. Later, in Trunk, I add a Status property to Widget and create Migration3. This migration is applied to the production database (thus cannot be removed and recreated in another branch).

     public class Widget { public int Id { get; set; } public string Status { get; set; } } public partial class Migration3 : DbMigration { public override void Up() { AddColumn("dbo.Widgets", "Status", c => c.String()); } //... } 
  6. BigProjectBranch is merged back into Trunk. We now have Migration3 applied to the database, and Migration1 and Migration2, which are chronologically earlier but not applied to the database.

     public class Widget { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public string Status { get; set; } } 
  7. Following the advice in http://coding.abel.nu/2012/02/ef-migrations-and-a-merge-conflict/ and several SO answers, I attempted to re-scaffold Migration1 and get an error:

     PM> Add-Migration Migration1 Unable to generate an explicit migration because the following explicit migrations are pending: [201311032026211_Migration1, 201311032027440_Migration2]. Apply the pending explicit migrations before attempting to generate a new explicit migration. 
  8. I tried the same for Migration2, which succeeded:

     PM> Add-Migration Migration2 Re-scaffolding migration 'Migration2'. Only the Designer Code for migration 'Migration2' was re-scaffolded. To re-scaffold the entire migration, use the -Force parameter. 

    Migrations lets me apply Migration2 to the database now, but this would result in migrations being applied in the wrong order (ie Migration2 before Migration1).

  9. Unfortunately, trying Migration1 again results in the same error as step 7.

Any ideas on resolving this please?

Seems like your migration history table is out of sync. See this Entity Framework Code Migrations - Stuck on Initial Migration and Julie Lermans link to get a better sense of how to start over now that the changes have been merged from the branch to trunk.

I would also recommend that since the there is one schema and two projects consider a common project that is merged from source to branch that contains both sets of changes, merge, reemerge in this case will lead to problems as your current question demonstrates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM