【发布时间】:2014-12-13 05:19:23
【问题描述】:
您好,我正在尝试在 Entity Framework 6 中建立代码优先关系,并且想知道如何执行以下操作。
我有一个Lead 课程和一个Order 课程。 Order 类必须有一个 Lead,但 Lead 可以在没有订单的情况下创建,并且不必关联到一个,因为用户可以在下订单之前离开。
所以我的班级结构是这样的:
public Lead
{
public int ID { get; set; }
...other non-related properties
}
public Order
{
public int ID { get; set; }
public int LeadID { get; set; }
public virtual Lead Lead { get; set; }
...other non-related properties
}
我尝试使用
将它们链接到OrderConfiguration()
this.HasRequired(o => o.Lead)
但这需要WithOptional 或Withrequired... 之类的东西来关注它,而不是HasForeignKey
是否有任何方法可以链接这些,以便 Order 中自动填充 Lead 而无需 Lead 类中的 Order 属性?
【问题讨论】:
标签: entity-framework code-first foreign-key-relationship