Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 685
Jaime Sangcap Avatar asked Feb 22 '14 08:02

Jaime Sangcap


People also ask

How do I reset my ef migration?

Resetting all migrations This can be easily done by deleting your Migrations folder and dropping your database; at that point you can create a new initial migration, which will contain your entire current schema.

How do I revert an applied migration in ef core?

Reverting a Migration In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot. > dotnet ef database update MyFirstMigration.


2 Answers

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
like image 199
Richard Avatar answered Sep 19 '22 11:09

Richard


I'm not sure if this helps anyone else, but I was able to edit and re-apply a migration by performing the following:

  1. Delete the record of the migration from the _MigrationHistory table. Find the one with a "migrationId" that matches your migration class file name and delete it.
  2. Manually undo all of the previous migration's changes (which would have been everything in the Down() section of your migration class file before you added the new stuff).
  3. Update-Database (or whatever you do to apply your migrations. I have auto-migration turned on so I just run my app).

My migration was pretty simple so I'm not sure if this will work for everyone.

Enjoy!

like image 24
Capt. Rochefort Avatar answered Sep 17 '22 11:09

Capt. Rochefort