实体框架核心2.1:外键约束导致循环/多级联路径

时间:2018-09-25 13:23:23

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

我想要的:

-如果删除FeedGroup,则删除与其关联的FeedGroupStatusesBinFeedGroupStatuses

-如果删除FeedGroupStatus,则删除与其关联的BinFeedGroupStatuses,但不删除与其关联的FeedGroup

-如果删除BinFeedGroupStatus,则不会删除任何关联对象,而只会删除关系。


我所拥有的:

-如果删除了FeedGroup,则FeedGroupId的{​​{1}}列将设置为FeedGroupStatus

-如果删除了NULL,则关联的FeedGroupStatus也将删除


我的处理方式:

我认为我的错误是我拥有BinFeedGroupStatus的{​​{1}}属性作为FeedGroupId而不是FeedGroupStatus。所以我改变了这一点,做了一个int?,但是当我尝试更新数据库时,出现了以下错误

  

在表'FeedGroupStatuses'上引入FOREIGN KEY约束'FK_FeedGroupStatuses_FeedGroups_FeedGroupId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。


我的问题:

我对here有一个普遍的了解,是什么导致上述错误,但是我不知道在我的代码中问题出在哪里。帮助吗?


模型

int


DataContext

add-migration


迁移

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

    public ICollection<FeedGroupStatus> FeedGroupStatuses { get; set; }

    //and other unrelated properties
}

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

    public int FeedGroupId { get; set; }  //<-- was nullable
    public FeedGroup FeedGroup { get; set; }

    public ICollection<BinFeedGroupStatus> BinFeedGroupStatuses { get; set; }

    //and other unrelated properties
}

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

    public int FeedGroupStatusId { get; set; }
    public FeedGroupStatus FeedGroupStatus { get; set; }
}


定义

FeedGroup:一组牲畜
FeedGroupStatus:该FeedGroup的员工人数以及它们在特定时间段内从中获取的垃圾箱。
BinFeedGroupStatus:Feed Bin与FeedGroup状态之间的关联。

0 个答案:

没有答案
相关问题