【问题标题】:Entity Framework Code First with one class having two references to anotherEntity Framework Code First,一个类有两个对另一个的引用
【发布时间】:2012-05-12 19:35:30
【问题描述】:

我喜欢 EF Code First,但有时似乎只在 SQL 中定义表会更容易。

在这种情况下,我有两个模型,如下所示:

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<BookRecommendation> BookRecommendations { get; set; }
    // additional properties about the item
}

public class BookRecommendation
{
    public int Id { get; set; }
    // public int BookId { get; set; } // from attempts to use data annotations
    public virtual Book Book { get; set; }
    // public int BookRecommendedId { get; set; } // from attempts to use data annotations
    public virtual Book BookRecommended { get; set; }
    // additional properties about the relationship between the two items
}

不幸的是,我无法让它与数据注释或流式 API 一起正常工作。

Multiple foreign keys pointing to same table in Entity Framework 4.1 code firstEntity Framework 4.1 InverseProperty Attribute and ForeignKey之类的问题,但这些问题往往涉及两端的集合。

我的模型可能是错误的,因为很早就开始感到沮丧,我考虑如何在 SQL 中做到这一点:

Book
    Id
    Name
    // other properties

Recommendation
    Id
    BookId
    RecommendedBookId
    // other properties

Book.Id : Recommendation.BookIdBook.Id : Recommendation.RecommendedBookId 之间会有外键。

我需要通过数据注释或流畅的 API 做什么才能使其正常工作,或者我应该如何修改我的模型?

【问题讨论】:

  • 老实说,鉴于这个问题已经超过 4 年了,我不能说 100% 是否可能与该问题重复。但是基于该问题的两个最高评分的答案,以及 13 年 3 月对这个问题的最后一个答案,我似乎没有得到例外,但可能与我的数据库优先假设不匹配的表,我会倾向于这些与众不同。
  • “可能重复”是一种清理方法 - 关闭类似问题并保留最佳答案。见meta.stackexchange.com/questions/147643/…

标签: ef-code-first


【解决方案1】:
public class Book
{
    public int BookID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<BookRecommendation> BookRecommendations { get; set; }

}

public class BookRecommendation
{
    public int BookRecommendationID { get; set; }
    public int BookID { get; set; } 
    public string remarks { get; set; } //Some recommendation text

    public virtual Book Book { get; set; }

}

我想这应该可以解决问题!它将为它创建一个 Book 实体和一个 Recommendations 集合。这意味着一本书可以有很多推荐,而推荐只属于一本书。当一本书被创建,然后你尝试为它写推荐时,你会看到有一个下拉框,通过它的“BookID”来显示书名。

【讨论】:

    【解决方案2】:

    好问题。抱歉,我花了一年时间才找到这个。

    您需要使用InversePropertyAttribute 告诉 EF 您与 Book 的 2 个关系中的哪一个是 BookRecommendation 指向的那个。在代码中:

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        [InverseProperty("BookRecommended")]
        public virtual ICollection<BookRecommendation> BookRecommendations { get; set; }
        // additional properties about the item
    }
    
    public class BookRecommendation
    {
        public int Id { get; set; }
    
        [ForeignKey("Book")]
        public int BookId { get; set; }
        public virtual Book Book { get; set; }
    
        [ForeignKey("BookRecommended")]
        public int BookRecommendedId { get; set; }
        public virtual Book BookRecommended { get; set; }
    }
    

    因此,Book 上的 InverseProperty 将 BookRecommended 属性命名为 BookRecommendation 上的属性,因此 EF 可以清楚地知道这指的是 2 个 FK 中的哪一个。为了更好地衡量,2 ForeignKey 属性可以显式命名 BookRecommendation 上的 FK 属性 - 如果需要,您可以摆脱额外的属性,但如果您保留它们,则该属性必须存在。

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 2018-05-04
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      相关资源
      最近更新 更多