实体框架核心2.1:OnDelete级联-多个级联路径

时间:2018-10-05 16:34:23

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

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

  

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

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

我知道有关此的其他文章,但是我还没有找到可以真正帮助我理解这一概念的解释。您能否在以下两个示例中说明如何在OnModelCreating中格式化DbContext以便允许所需的功能?


示例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”时出现

错误

我想要的功能:删除Barn时,关联的Bins也被删除。删除Gateway时,删除关联的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也将删除。

1 个答案:

答案 0 :(得分:0)

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