How to edit a previously applied migration without first adding the first migration to the EF code

I have application migration using the "haward" db scheme.

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 don't want to add migration with this, so I thought just editing 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 the empty database and issue the update-database command but it says that I still have pending changes.

therefore, the add-migration command was issued to me to check which particular changes. and it looks like it still detects my changes (from "haward" to "tr") even without a migration table.

? ? , , . , , .

+6
3

TL; DR: - , .

Entity Framework - . , , , - .

, . MigrationName.designer.cs. migration.cs, . .

- .

3 :

  • Migration1
  • Migration2
  • Migration3

Migration2 ...

  • Migration1: Update-Database -TargetMigration Migration1 -Force (NB - , ).
  • , 2, 2
  • Migration2: Add-Migration xxxxxxxxxxx_Migration2 ( , ). designer.cs.
  • Migration2: Update-Database -TargetMigration Migration2
  • .
  • Migration3: Add-Migration xxxxxxxxxxx_Migration3
  • : Update-Database
+18

, - , , :

  • _MigrationHistory. "migrationId", .
  • ( "" ) , ).
  • ( . , ).

, , .

!

+4

, , (Git, Subversion ..), . , .

, :

  • Migration1
  • Migration2
  • Migration3

, Migration2, :

  1. , Migration2
  2. , ( Migration3 - , Migration2)
  3. Migration1 - Update-Database -TargetMigration Migration1 -Force
  4. , 1
  5. Recreate Migration2 - Add-Migration Migration2(now it will contain exactly the changes you want)
  6. Copy the created migration files to another directory
  7. Discard all uncommitted changes
  8. Check HEAD commit again
  9. Delete files for Migration2 and Migration3
  10. Copy the files from step 6 back to their source directory
  11. Update the database - Update-Database
  12. Re- create Migration 3 -Add-Migration Migration3
  13. Update the database again - Update-Database
0
source

All Articles