开发者

many to many relationship in EF 4.1

开发者 https://www.devze.com 2023-04-13 05:50 出处:网络
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.

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 F开发者_运维技巧irst 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; }
        }


    }


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; }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号