【问题标题】:Is there any relationship between Foreign-key in the underline tables and the navigation properties in the model?下划线表中的外键与模型中的导航属性之间是否有任何关系?
【发布时间】:2011-08-20 15:27:04
【问题描述】:

我对 EF Code First 中的导航属性有点困惑。

以下2个类来自ScottGu tutorial on EF Code First

public class Dinner
{
  public int DinnerID { get; set; }
  public string Title { get; set; }
  public DateTime EventDate { get; set; }
  public string Address { get; set; }
  public string HostedBy { get; set; }   

  public virtual ICollection<RSVP> RSVPs { get; set; }   
}

public class RSVP
{
  public int RsvpID { get; set; }
  public int DinnerID { get; set; }
  public string AttendeeMail { get; set; }

  public virtual Dinner Dinner { get; set; }
}

它们是 ScottGu 的 NerdDinner 教程中的 2 张桌子:DinnersRSVP。 DinnerID 列是晚餐的主键和 RSVP 的外键

这些是我的问题:

  1. 我明白为什么在 Diner 类中有 ICollection RSVPs 属性,但为什么在 RSVP 类中有 Dinner Dinner 属性?
  2. 如果我不在 RSVP 类中添加 DINER 导航属性,会发生什么?

我可以这样总结我所有的问题:“下划线表中的外键和模型中的导航属性之间没有任何关系吗?”

导航属性是否可以反映underline数据库中表之间的关系?

很抱歉这么长,我真的很困惑。

感谢您的帮助

【问题讨论】:

标签: entity-framework-4.1


【解决方案1】:
  1. Diner 类中的

    ICollection RSVPs 属性和 RSVP 类中的 Dinner 属性是导航属性 这表明两者之间存在一对多的关系 DinerRSVP。是的,导航属性可以反映这种关系。

    2.你可以这样做。您应该至少有一个导航属性来映射关系。 EF4 Code First: how to add a relationship without adding a navigation property

【讨论】:

  • 至少我知道有关系。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
相关资源
最近更新 更多