简体   繁体   English

Entity Framework Core 中的种子脚本正在破坏我的迁移

[英]Seed script in Entity Framework Core is breaking my migration

I have a very simple scenario where I have number of related entities and a couple of them are expected to be prefilled (through seed script).我有一个非常简单的场景,其中我有许多相关实体,并且其中一些预计会被预填充(通过种子脚本)。 Then, I run my application and it adds a new related data (related to the tables populated by seed script).然后,我运行我的应用程序,它添加了一个新的相关数据(与种子脚本填充的表相关)。

And eventually, when I want to add a new migration, for example make a field nullable (of an entity which was not seeded), I try the update-database command and everything breaks up because the migration is trying to recreate my seeded tables.最终,当我想添加一个新的迁移时,例如使一个字段可以为空(一个没有种子的实体),我尝试了update-database命令并且一切都崩溃了,因为迁移试图重新创建我的种子表。 It is impossible because they keep a foreign key.这是不可能的,因为他们保留了外键。

I will be more specific:我会更具体:

Car (Id, Brand, Color, Year)汽车(ID、品牌、颜色、年份)

many-to-one多对一

CarType (Id, Value)汽车类型(ID、值)

So, in my DbContext I have the following:因此,在我的 DbContext 中,我有以下内容:

modelBuilder.Entity<CarType>()
                .HasData(new CarType('4125ad9e-68fe-4d25-9d73-7e8acc097d6f', 'Coupe'))

And then, I run my application and I start inserting new Car s with the respective types.然后,我运行我的应用程序并开始插入具有相应类型的新Car So, now I have:所以,现在我有:

Car(1, 'BMW', 'Black', 2011) FK-> CarType('4125ad9e-68fe-4d25-9d73-7e8acc097d6f', 'Coupe')
Car(2, 'Audi', 'Green', 2008) FK-> CarType('cc097d6f9e-68fe-4d25-9d73-4125ad7e8a', 'Estate')

But, one day I decide to make the Year field nullable但是,有一天我决定让Year字段为空

public int? Year { get; set; }

I ran add-migration and everything looks fine except for that in the Up statement:我运行了add-migration ,一切看起来都很好,除了Up语句中的内容:

migrationBuilder.DeleteData(
                table: "CarType",
                keyColumn: "Id",
                keyValue: new Guid("4125ad9e-68fe-4d25-9d73-7e8acc097d6f"));

Once I execute update-database I get an error that the operation can not be executed because there's a foreign key related to the entity I am trying to delete:执行update-database后,我会收到无法执行操作的错误,因为存在与我要删除的实体相关的外键:

The DELETE statement conflicted with the REFERENCE constraint DELETE 语句与 REFERENCE 约束冲突

How am I supposed to handle that?我该怎么处理? My seed scrips are kind of blocking me right now.我的种子票现在有点挡住了我。 I want to be able to make changes to my other tables, even on the ones that are seed scripted without the need to recreate stuff.我希望能够对我的其他表进行更改,即使是在那些不需要重新创建内容的种子脚本上。

I think you've records in DB depends on Card Type Id with the same Id you tried to delete so you can manually delete all dependent entities from DB then apply update database and this not recommended or change all entities that have relation with CardType to be "Cascade" in onModelCreating function我认为您在 DB 中的记录取决于 Card Type Id 与您尝试删除的 Id 相同,因此您可以手动从 DB 中删除所有相关实体,然后应用更新数据库,这不推荐或将所有与 CardType 有关系的实体更改为onModelCreating function 中的“级联”

example例子

entity.HasOne(d => d.CardTypes)
                .WithMany(p => p.Cards)
                .HasForeignKey(d => d.CardTypeId)
                .OnDelete(DeleteBehavior.Cascade)   /* Cascade */
                .HasConstraintName("FK_Cards_CardTypes");

The solution for me was to remove the HasData related seeding from my DbContext because when a migration is added the changes to the data specified with HasData are transformed to calls to InsertData (), UpdateData (), and DeleteData ().我的解决方案是从我的 DbContext 中删除与HasData相关的种子,因为当添加迁移时,对使用HasData指定的数据的更改会转换为对InsertData ()、 UpdateData () 和DeleteData () 的调用。

So, as suggested HERE , I moved the initialization logic into my initial migration.所以,正如这里所建议的,我将初始化逻辑移到了我的初始迁移中。

migrationBuilder.InsertData(
    table: "CarType",
    columns: new[] { "Id", "Value" },
    values: new object[] { "4125ad9e-68fe-4d25-9d73-7e8acc097d6f", "Coupe" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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