【问题标题】:Introducing FOREIGN KEY constraint 'FK_XY' on table 'XY' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION在表 'XY' 上引入 FOREIGN KEY 约束 'FK_XY' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION
【发布时间】:2021-05-08 15:07:52
【问题描述】:

我有Affaire,可以有多个Zone

public class Affaire {
    // ...
    public ICollection<Zone> Zones { get; set; };
}

public class Zone {
    // ...
    public ICollection<Affaire> Affaires { get; set; };
}

流畅的Zone 配置(仅限Zone):

public override void Configure(EntityTypeBuilder<Zone> builder)
{
    // ...
    builder
        .HasMany(p => p.Affaires)
        .WithMany(p => p.Zones)
        .UsingEntity(j => j.ToTable("Affaire_Zone"));
}

这是我生成的迁移:

migrationBuilder.CreateTable(
    name: "Affaire_Zone",
    columns: table => new
    {
        AffairesId = table.Column<int>(type: "int", nullable: false),
        ZonesId = table.Column<int>(type: "int", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Affaire_Zone", x => new { x.AffairesId, x.ZonesId });
        table.ForeignKey(
            name: "FK_Affaire_Zone_Affaire_AffairesId",
            column: x => x.AffairesId,
            principalTable: "Affaire",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_Affaire_Zone_Zone_ZonesId",
            column: x => x.ZonesId,
            principalTable: "Zone",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

add-migration 命令通过 OK。但是当我做update-database 时,我得到了错误

引入 FOREIGN KEY 约束“FK_Affaire_Zone_Zone_ZonesId” 表 'Affaire_Zone' 可能会导致循环或多个级联路径。 指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 外键约束。无法创建约束或索引。看 以前的错误。

【问题讨论】:

    标签: c# .net entity-framework entity-framework-core ef-core-5.0


    【解决方案1】:

    通过手动更新迁移代码部分解决,但是当我进行另一次迁移时,它将删除此代码

    改变了

    onDelete: ReferentialAction.Cascade);
    

    onDelete: ReferentialAction.NoAction);
    

    这里:

    migrationBuilder.CreateTable(
        name: "AffaireZone",
        columns: table => new
        {
            AffairesId = table.Column<int>(type: "int", nullable: false),
            ZonesId = table.Column<int>(type: "int", nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_AffaireZone", x => new { x.AffairesId, x.ZonesId });
            table.ForeignKey(
                name: "FK_AffaireZone_Affaire_AffairesId",
                column: x => x.AffairesId,
                principalTable: "Affaire",
                principalColumn: "Id",
                onDelete: ReferentialAction.NoAction);
            table.ForeignKey(
                name: "FK_AffaireZone_Zone_ZonesId",
                column: x => x.ZonesId,
                principalTable: "Zone",
                principalColumn: "Id",
                onDelete: ReferentialAction.NoAction);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 2011-05-08
      • 2013-10-22
      • 2020-12-20
      • 2021-05-30
      相关资源
      最近更新 更多