表" XY"上引入外键约束" FK_XY"可能会导致周期或多个级联路径.在删除无操作上指定[英] Introducing FOREIGN KEY constraint 'FK_XY' on table 'XY' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION

本文是小编为大家收集整理的关于表" XY"上引入外键约束" FK_XY"可能会导致周期或多个级联路径.在删除无操作上指定的处理方法,想解了表" XY"上引入外键约束" FK_XY"可能会导致周期或多个级联路径.在删除无操作上指定的问题怎么解决?表" XY"上引入外键约束" FK_XY"可能会导致周期或多个级联路径.在删除无操作上指定问题的解决办法?表" XY"上引入外键约束" FK_XY"可能会导致周期或多个级联路径.在删除无操作上指定问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我有Affaire可以具有多个Zone

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

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

Fluent 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命令通过确定.但是当我做update-database时,我会得到错误

介绍外键约束'fk_affaire_zone_zone_zone_zonesid'on 表" affaire_zone"可能会导致周期或多个级联路径. 在删除no操作或更新no操作上指定或修改其他操作 外国钥匙限制.无法创建约束或索引.看 以前的错误.

推荐答案

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

更改了

onDelete: ReferentialAction.Cascade);

to

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);
    });

本文地址:https://www.itbaoku.cn/post/2254469.html

问题描述

I have Affaire that can have multiple Zone

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

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

the fluent Zone configuration (only on Zone) :

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

Here is my generated migration:

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);
    });

The add-migration command passes OK. but when I do update-database I get the error

Introducing FOREIGN KEY constraint 'FK_Affaire_Zone_Zone_ZonesId' on table 'Affaire_Zone' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

推荐答案

Solved partially by manually updating the Migration code, but when I will do another migration it will remove this code

changed the

onDelete: ReferentialAction.Cascade);

to

onDelete: ReferentialAction.NoAction);

here:

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);
    });
查看更多