EF 4.1 中的多对多关系

发布于 2024-12-09 18:46:03 字数 1445 浏览 0 评论 0原文

我只是在做一个有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-12-16 18:46:03

如果您将连接表建模为单独的实体,则该实体与其他 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; }
    }
}

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.

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; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文