实体框架中的外键 - 循环或多级联路径错误

发布于 2024-12-13 05:04:54 字数 1653 浏览 1 评论 0原文

例如,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-12-20 05:04:54

正如 Ladislav 提到的,您正在为 BlogMeta 实体定义多个级联路径。您必须为您的其中一种关系禁用级联。

您可以将以下方法添加到上下文类中,以禁用 User-BlogMeta 关系的级联:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<BlogMeta>().HasRequired(bm => bm.User).WithMany().WillCascadeOnDelete(false);
    base.OnModelCreating(modelBuilder);
}  

您可以指示关系的另一端 (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:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<BlogMeta>().HasRequired(bm => bm.User).WithMany().WillCascadeOnDelete(false);
    base.OnModelCreating(modelBuilder);
}  

You can indicate the other end of relationship (WithMany(u => u.BlogMetas)) if you have defined a colletion of BlogMeta in your User class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文