EF 4.1 中的多对多关系
我只是在做一个有 3 个表的小应用程序。
申请人 职位 每个职位的申请人。
最后一个表与其他两个表是多对多关系。
不过,我正在使用“代码优先”方法,但我不确定我正在做的代码是否正确。
我在其他 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; }
}
}
I am just doing a small application with 3 tables.
Applicants
Positions
ApplicantsPerPosition.
This last one would be a many to many relationship with the other 2 tables.
However I am using CODE First approach, but I am not sure if I what I am doing code is correct or not.
I added a ICollection and ICollection in the other 2 tables.
Is this correct or not? I did it this way, to be able to navigate through the properties of associated objects easily, however I am not sure if at the end this will be translated into 3 tables only as I would do it in a DATABASE First approach.
Example code is here:
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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将连接表建模为单独的实体,则该实体与其他 2 个实体之间存在一对多关系。您还需要将主键列公开为链接实体中的属性。
If you are modeling the join table as a separate entity then you have one-to-many relationships between that entity and the other 2 entities. You also need to expose the primary key columns as properties in the link entity.