实体框架代码首先:FOREIGN KEY约束可能会导致循环或多个级联路径 [英] Entity Framework Code First: FOREIGN KEY constraint may cause cycles or multiple cascade paths

查看:194
本文介绍了实体框架代码首先:FOREIGN KEY约束可能会导致循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架代码首先可以为以下POCO生成数据库。

Entity Framework Code First can generate the DB for the following POCOs.

public class Item {
    public int Id { get; set; }
    public string Name { get; set; }
}

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

    public virtual Item FirstItem { get; set; }
    public virtual Item SecondItem { get; set; }
}

我想通过ID字段与First和Second项目建立关系而不是整个项目类。所以:

I would like to establish the relationship with First and Second item via ID fields rather than the an entire "Item" class. So:

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

    public virtual Item FirstItem { get; set; }
    public int FirstItem_Id { get; set; }

    public virtual Item SecondItem { get; set; }
    public int SecondItem_Id { get; set; }
}

也可以使用。 编辑:这实际上不起作用。只需生成额外的FirstItem_Id1和SecontItem_Id2列。

also works. Edit: This didn't actually work. Just generates additional FirstItem_Id1 and SecontItem_Id2 columns.

但是只需将外键属性更改为FirstItemId,SecondItemId(不带下划线)就如此:

But just changing the foreign key properties to FirstItemId, SecondItemId, (without the underscore) like so:

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

    public virtual Item FirstItem { get; set; }
    public int FirstItemId { get; set; }

    public virtual Item SecondItem { get; set; }
    public int SecondItemId { get; set; }
}

导致以下异常。

{"Introducing FOREIGN KEY constraint 'ItemPair_SecondItem' on table 'ItemPair' may cause
cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION,
or modify other FOREIGN KEY constraints.\r\nCould not create constraint. 
See previous errors."}

为什么?我可以做些什么来避免这个例外。

Why? And what can I do to avoid this exception.

推荐答案

我的期望是,在第一种情况下,您的Id属性不被用于数据库作为FK和EF将创建另外两列(您可以使用 ForeignKeyAttribute )强制与FK属性配对导航属性来验证此列。在第二种情况下,EF将正确识别您的属性,但它也将使用级联删除约定,这将导致SQL Server中的错误。表中有两个属性指向同一个父项。实际上在数据库中,您可以从相同的项目(两个FK设置为​​相同的Id)创建 ItemPair 。如果两个关系都启用了级联删除,则会在SQL服务器中导致多个级联路径=>不允许。

My expectation is that in the first case your Id properties are not used in the database as FKs and EF will create another two columns (you can validate this by forcing pairing of navigation property with FK property using ForeignKeyAttribute). In the second case EF will correctly recognize your properties but it will also use cascade delete convention which will cause error in SQL server. You have two properties from the table pointing to the same parent. Actually in the database you can createItemPair from the same Item (both FKs set to the same Id). If both relations have cascade delete enabled it will result in multiple cascade paths => not allowed in SQL server.

此处的解决方案是流畅的映射以手动定义关系如何映射。 这里是一个例子。

The solution here is fluent mapping to manually define how the relations are mapped. Here is the example.

这篇关于实体框架代码首先:FOREIGN KEY约束可能会导致循环或多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆