实体框架中的外键 - 循环或多级联路径错误
例如,使用 EF Code First 我有以下内容:
public class Blog
{
public int BlogID { get; set; }
public string Content { get; set; }
public virtual User User { get; set; }
public virtual ICollection<BlogMeta> BlogMeta { get; set; }
}
public class BlogMeta
{
public int BlogMetaID { get; set; }
public string Content { get; set; }
public virtual User User { get; set; }
public virtual Blog Blog { get; set; }
}
这成功生成了表 Blog 和 BlogMeta 并创建了与 User 表的外键关系。阅读后 这个我将其更改为以下内容:
public class Blog
{
public int BlogID { get; set; }
public string Content { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public virtual ICollection<BlogMeta> BlogMeta { get; set; }
}
public class BlogMeta
{
public int BlogMetaID { get; set; }
public string Content { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public int BlogID { get; set; }
public virtual Blog Blog { get; set; }
}
现在它不起作用。它生成表,然后在尝试创建关系时抛出以下错误:
在表“BlogMeta”上引入 FOREIGN KEY 约束“BlogMeta_User”可能会导致循环或多个级联路径。
那么引入public int UserID
有什么好处,为什么会失败呢?
编辑: 好的,所以我遇到了 这个答案,其中概述了独立关联和外键关联之间的区别......事实证明这就是我所说的。那么这就留下了一个问题,为什么在使用外键关联时会抛出上述错误?
Using EF Code First i have the following, for example:
public class Blog
{
public int BlogID { get; set; }
public string Content { get; set; }
public virtual User User { get; set; }
public virtual ICollection<BlogMeta> BlogMeta { get; set; }
}
public class BlogMeta
{
public int BlogMetaID { get; set; }
public string Content { get; set; }
public virtual User User { get; set; }
public virtual Blog Blog { get; set; }
}
This successfully generates the tables Blog and BlogMeta and creates the foreign key relationship with the User table. After reading this i changed this to the following:
public class Blog
{
public int BlogID { get; set; }
public string Content { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public virtual ICollection<BlogMeta> BlogMeta { get; set; }
}
public class BlogMeta
{
public int BlogMetaID { get; set; }
public string Content { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public int BlogID { get; set; }
public virtual Blog Blog { get; set; }
}
and now it doesn't work. It generates the tables and then throws the following error when trying to create the relationships:
Introducing FOREIGN KEY constraint 'BlogMeta_User' on table 'BlogMeta' may cause cycles or multiple cascade paths.
So what is the advantage of introducing the public int UserID
and why does it fail when doing so?
EDIT:
Ok, so i've come across this answer which outlines the difference between Independent Associations and Foreign Key Associations... which it turns out is what i was talking about. So this leaves the question, why does it throw the above error when using foreign key associations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Ladislav 提到的,您正在为
BlogMeta
实体定义多个级联路径。您必须为您的其中一种关系禁用级联。您可以将以下方法添加到上下文类中,以禁用 User-BlogMeta 关系的级联:
您可以指示关系的另一端 (
WithMany(u => u.BlogMetas)
),如果在您的User
类中定义了BlogMeta
的集合。As Ladislav mentioned, you are defining multiple cascade paths for
BlogMeta
entity. You'd have to disable cascade for one of your relationships.You can add the following method to your context class, to diable cascade for your User-BlogMeta relationship:
You can indicate the other end of relationship (
WithMany(u => u.BlogMetas)
) if you have defined a colletion ofBlogMeta
in yourUser
class.