【问题标题】:Lead to order relationship code first先引出订单关系码
【发布时间】: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)

但这需要WithOptionalWithrequired... 之类的东西来关注它,而不是HasForeignKey

是否有任何方法可以链接这些,以便 Order 中自动填充 Lead 而无需 Lead 类中的 Order 属性?

【问题讨论】:

    标签: entity-framework code-first foreign-key-relationship


    【解决方案1】:

    您必须为 forgin 键设置属性,它必须看起来像这些

    [Key]
    public int ID { get; set; }
    [ForeignKey("Lead")]
    public int LeadID { get; set; }
    

    在Lead类中

    也设置密钥

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多