How to edit previously applied migration without adding another migration in EF code first

ef-code-first ef-migrations entity-framework entity-framework-6

Question

I have an applied migration using "haward" db schema.

public partial class CreateCourseCategoryTable : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "haward.CourseCategories",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                        Code = c.String(),
                    })
                .PrimaryKey(t => t.Id);
        }

        public override void Down()
        {
            DropTable("haward.CourseCategories");
        }
    }

with this mapping

public class CourseCategoryMapping : EntityTypeConfiguration<CourseCategory>
    {
        public CourseCategoryMapping()
        {
            ToTable("CourseCategories", "haward");
        }
    }

now I want to change the schema from "haward" to "tr" I dont want to add migration with this one so I thought of just editing directly the source code of the Migration and Mapping.

public partial class CreateCourseCategoryTable : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "tr.CourseCategories",
                    c => new
                        {
                            Id = c.Int(nullable: false, identity: true),
                            Name = c.String(),
                            Code = c.String(),
                        })
                    .PrimaryKey(t => t.Id);
            }

            public override void Down()
            {
                DropTable("tr.CourseCategories");
            }
        }


    public class CourseCategoryMapping : EntityTypeConfiguration<CourseCategory>
        {
            public CourseCategoryMapping()
            {
                ToTable("CourseCategories", "tr");
            }
        }

then recreate empty database and issued the command update-database but its telling that I still have pending changes.

so what I did was issued the add-migration command to check which changes are those. and it seems like it still detects my edits(from "haward" to "tr" schema) even without the migrations table.

where do the model changes being saved? and how to edit directly the source code and reapply the migration? I know its not advisable because thats what migration is for. but I dont want to get my history dirty with just those changes specially if I am only at early development stage.

1
8
2/22/2014 8:19:09 AM

Popular Answer

TL;DR: This is very complicated - it's a lot easier just to add a new migration later to correct the problem.

An Entity Framework migration consists of two parts - the code, and a hash of the model. The hash of the model is used to determine whether the model has changed, and hence whether any new migrations are required.

If you change the model, you change the hash. This hash is stored in the MigrationName.designer.cs file. You cannot just edit the model and change the migration.cs code, as the model no longer matches the model hash. You also need to regenerate the hash for the model.

The only way to do this is to roll your database back, and update the hash.

Consider you have 3 migrations applied:

  • Migration1
  • Migration2
  • Migration3

If you want to apply a change from Migration2 onwards...

  • Roll back to Migration1: Update-Database -TargetMigration Migration1 -Force (NB - This may cause data loss, so work on a development copy of your database)
  • Make your model code match what you wanted for Migration 2, and update the code for Migration 2 by hand
  • Regenerate the designer file for Migration2: Add-Migration xxxxxxxxxxx_Migration2 (use the full name of the migration including the date). This will only update the designer.cs file
  • Apply Migration2: Update-Database -TargetMigration Migration2
  • Reapply any model changes to your code for Migration3
  • Regenerate the designer file for Migration3: Add-Migration xxxxxxxxxxx_Migration3
  • Update the database to latest: Update-Database
20
12/30/2016 8:08:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow