首页 > 解决方案 > Entity Framework Core 2.1:OnDelete Cascade - 多个级联路径

问题描述

尝试更新数据库时,我在许多不同的地方收到以下错误。

...可能导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

该错误似乎发生在我在一个对象上有多个一对多关系的地方。

我知道已经发表了其他帖子,但我还没有找到真正帮助我理解这个概念的解释。您能否在以下两个示例中解释我应该如何格式化OnModelCreatingDbContext实现所需的功能?


示例 1

课程

public class Bin
{
    public int Id { get; set; }

    public int BarnId { get; set; }
    public Barn Barn { get; set; }

    public int GatewayId { get; set; }
    public Gateway Gateway { get; set; }
}

public class Barn
{
    public int Id { get; set; }
    public ICollection<Bin> Bins { get; set; }
}

public class Gateway
{
    public int Id { get; set; }
    public ICollection<Bin> Bins { get; set; }
}


移民

migrationBuilder.CreateTable(
    name: "Bins",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
        BarnId = table.Column<int>(nullable: false),
        GatewayId = table.Column<int>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Bins", x => x.Id);
        table.ForeignKey(
            name: "FK_Bins_Barns_BarnId",
            column: x => x.BarnId,
            principalTable: "Barns",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_Bins_Gateways_GatewayId",
            column: x => x.GatewayId,
            principalTable: "Gateways",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
     });

尝试创建“FK_Bins_Gateways_GatewayId”时发生错误

我想要的功能:当 aBarn被删除时,关联Bins的也会被删除。当 aGateway被删除时,关联Bins不会被删除


示例 2

课程

public class Bin
{
    public int Id { get; set; }

    public ICollection<BinFeed> BinFeeds { get; set;}
}

public class Feed
{
    public int Id { get; set; }

    public ICollection<BinFeed> BinFeeds { get; set;}
}

public class BinFeed
{
    public int Id { get; set; }

    public bool Current { get; set;}

    public int BinId { get; set; }
    public Bin Bin { get; set; }

    public int FeedId { get; set; }
    public Feed Feed { get; set; }
}


移民

migrationBuilder.CreateTable(
            name: "BinFeeds",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        BinId = table.Column<int>(nullable: false),
        FeedId = table.Column<int>(nullable: false),
        Current = table.Column<bool>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_BinFeeds", x => x.Id);
            table.ForeignKey(
            name: "FK_BinFeeds_Bins_BinId",
            column: x => x.BinId,
            principalTable: "Bins",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_BinFeeds_Feeds_FeedId",
            column: x => x.FeedId,
            principalTable: "Feeds",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

尝试创建“FK_BinFeeds_Feeds_FeedId”时发生错误

我想要的功能:当Bin被删除时,关联BinFeeds被删除。当Feed被删除时,关联BinFeeds被删除。

标签: asp.net-core-2.1entity-framework-core-2.1

解决方案


我过去有类似的问题。我发现如果您不手动配置OnModelCreating它会自动找到关系并且您没有问题(可能它在选项 2 中做同样的事情)。如果您以后不打算从该关系中的表中删除任何数据,您也可以设置onDelete:ReferentialAction.NoAction 。其他解决方案是使用第三方包的触发器或通过 SQL 手动配置。


推荐阅读