我在 OnModelCreating 方法上有这段代码:
// Users
modelBuilder.Entity<User>().ToTable(name: "Users", schema: "Core");
modelBuilder.Entity<User>().HasKey(x => x.Id);
// Seeder
modelBuilder.Entity<User>().HasData(MigrationSeeder.Seed());
当我生成迁移时,EF会像这样创建插入操作:
migrationBuilder.InsertData(
schema: "Core",
table: "Users",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { "6c6aa94903a4", "Rick", "Sánchez" });
好吧!
但..当我生成另一个迁移时,EF重复此插入操作(在删除行之前和插入新行之前)
migrationBuilder.DeleteData(
schema: "Core",
table: "Users",
keyColumn: "Id",
keyValue: "6c6aa94903a4");
migrationBuilder.InsertData(
schema: "Core",
table: "Users",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { "d055353aa45c", "Rick", "Sánchez" });
如果与主表和数据库对应的此实体在另一个表中具有相关行。
如何避免在每次迁移中插入?