【问题标题】:Code First Navigation Property 1 to 1 Relationship with Two Foreign KeysCode First 导航属性与两个外键的一对一关系
【发布时间】:2014-01-26 20:51:15
【问题描述】:

我有以下课程:

public class ParticipantAssignment
{
    [Key]
    public int ParticipantId { get; set; }
    public int WorksitePositionId { get; set; }
    public int WorksiteId { get; set; }
    public int EmployerId { get; set; }
    public int ProgramId { get; set; }
    public int UserId { get; set; }
    public DateTime AssignDateTime { get; set; }

    [Not Sure What Attribute to Use Here to Create the Relationship]
    public ProgramWorksite ProgramWorksite { get; set; }
    ... removed ...
}


public class ProgramWorksite
{

    [Key, Column(Order = 1)]
    public int WorksiteId { get; set; }
    [Key, Column(Order = 2)]
    public int ProgramId { get; set; }
    ... removed ...
}

ParticipantAssignment 和 ProgramWorksite 之间存在一对一的关系。

我希望能够将ParticipantAssignment 类中的ProgramWorksite 导航属性与ProgramWorksite 类相关联。

但是,如果您注意到,这种关系将基于两个类中的两个字段,WorksiteIdProgramId - 不太确定如何执行此操作。

需要注意的一点...

上面的代码似乎正在工作,但我不确定它是否真的在关系中使用“ProgramId”,因为可以单独使用 WorksiteId 建立关系 - 它会提供记录但它不会在不考虑 ProgramId 的情况下保持准确

【问题讨论】:

  • 我认为你必须使用fluent api来解决问题。
  • 是的 - 我不会感到惊讶,我只是没有太多运气搜索谷歌,我什至不确定如何在一个搜索短语中描述我的问题,我使用的短语到目前为止还没有提出相关的结果。

标签: c# asp.net entity-framework ef-code-first


【解决方案1】:

好的 - 我能够分析使用 Glimpse 生成的 SQL,并且在上面的当前设置中,它似乎在连接中正确使用了 ProgramIdWorksiteId

【讨论】:

    猜你喜欢
    • 2022-07-18
    • 1970-01-01
    • 2014-01-20
    • 2016-03-16
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多