1

我只是在做一个3表的小应用程序。EF 4.1中的多对多关系

申请人 职位 ApplicantsPerPosition。

最后一个与其他2个表格有多对多的关系。

但是我使用的代码第一种方法,但我不知道如果我在做什么代码是正确的或不正确。

我在其他2个表中添加了ICollection和ICollection。

这是正确与否?我这样做是为了能够轻松地浏览关联对象的属性,但是我不确定最终是否会将它转换为3个表格,正如我在数据库优先方法中所做的一样。

示例代码是在这里:

public class Position 
    { 
     public int id { get; set; } 
     [StringLength(20, MinimumLength=3)] 
     public string name { get; set; } 
     public int yearsExperienceRequired { get; set; } 
     public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 
    } 

    public class Applicant 
    { 
     public int ApplicantId { get; set; } 
     [StringLength(20, MinimumLength = 3)] 
     public string name { get; set; } 
     public string telephone { get; set; } 
     public string skypeuser { get; set; } 
     public ApplicantImage photo { get; set; } 
     public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 

    } 

    public class ApplicantPosition 
    { 
     public virtual ICollection<Position> appliedPositions { get; set; } 
     public virtual ICollection<Applicant> applicants { get; set; } 
     public DateTime appliedDate { get; set; } 
     public int StatusValue { get; set; } 

     public Status Status 
     { 
      get { return (Status)StatusValue; } 
      set { StatusValue = (int)value; } 
     } 


    } 

回答

2

如果你是作为一个独立的实体建模的连接表,那么你有实体和其他2个实体之间的一个一对多的关系。您还需要将主键列公开为链接实体中的属性。

public class Position 
{ 
    public int id { get; set; } 
    [StringLength(20, MinimumLength=3)] 
    public string name { get; set; } 
    public int yearsExperienceRequired { get; set; } 
    public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 
} 

public class Applicant 
{ 
    public int ApplicantId { get; set; } 
    [StringLength(20, MinimumLength = 3)] 
    public string name { get; set; } 
    public string telephone { get; set; } 
    public string skypeuser { get; set; } 
    public ApplicantImage photo { get; set; } 
    public virtual ICollection<ApplicantPosition> applicantPosition { get; set; } 

} 

public class ApplicantPosition 
{ 
    public int ApplicantId { get; set; } 

    public int PositionId { get; set; } 

    public virtual Position Position { get; set; } 

    public virtual Applicant Applicant { get; set; } 

    public DateTime appliedDate { get; set; } 
    public int StatusValue { get; set; } 

    public Status Status 
    { 
     get { return (Status)StatusValue; } 
     set { StatusValue = (int)value; } 
    } 
}