使用Code First创建多个FK关系

时间:2013-10-04 21:41:25

标签: c# entity-framework ef-code-first foreign-keys

在我的架构设计中,我有2个表由2个不同的键相关联。我知道当他们是桌子的“Id”成员时,EF神奇地创造了外键,但我不太明白如何建立第二种关系。

注意:此架构尚未存在;我将使用codefirst来创建它。

这是我想要做的简单图表(粉红色的关系是我不确定如何在codefirst中实现的)NewClub.Id是主键,所有NEW_CLUB_ID都是外键粉红色突出显示的列是相关的,但不是唯一的: enter image description here

    namespace NCB.Models
    {
     public  sealed class NewClub
     {


        public NewClub()
        {
            //Related tables here
            NewClubRecruitingTeams = new List<NewClubRecruitingTeam>();
            NewClubRecruitingTeamDates = new List<NewClubRecruitingTeamDate>();

        }

        //Primary Key
        public int Id { get; set; }

        //Club Info
        public string CreatorMasterCustomerId { get; set; }
        public string ClubMasterCustomerId { get; set; }
        public bool IsConverted { get; set; }
        public string NewClubName { get; set; }
        public string NewClubLocation { get; set; }
        public string NewClubType { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateUpdated { get; set; }


        // Related tables here
        public virtual List<NewClubRecruitingTeam> NewClubRecruitingTeams { get; set; }
        public virtual List<NewClubRecruitingTeamDate> NewClubRecruitingTeamDates { get; set; }

     }

     public class NewClubRecruitingTeamDate
    {
        //Primary Key
        public int Id { get; set; }
        //Foriegn Keys
        public int NewClubId { get; set; }
        public int NewClubRecruitingTeamTeamNumber { get; set; }

        public DateTime? StartDateTime { get; set; }
        public DateTime? EndDateTime { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateUpdated { get; set; }

        public NewClub NewClub { get; set; }
        public NewClubRecruitingTeam NewClubRecruitingTeam { get; set; }
    }

     public class NewClubRecruitingTeam
    {
        //Primary Key
        public int Id { get; set; }
        //Foreign Key
        public int NewClubId { get; set; }

        public int TeamNumber { get; set; }
        public string TeamMemberName { get; set; }
        public string TeamMemberEmail { get; set; }
        public string TeamMemberPhone { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateUpdated { get; set; }

        public NewClub NewClub { get; set; }


      }
   }

0 个答案:

没有答案