MVC 5 C# Entities modelBuilder with multiple tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pod
    Contributor
    • Sep 2007
    • 298

    MVC 5 C# Entities modelBuilder with multiple tables

    Hello

    I am trying to get the modelBuilder.En tity to link three tables: [Committees], [Members] and [Roles]

    Table [LNK_Member_Comm ittee_Role] is where members are assigned a certain role within a certain committee; it contains the primary keys from the three tables in question.

    Using the modelBuilder.En tity<Committee> code below, I am able to build entities that collects all the members assigned to a committee, and can also collect all committees a member belongs to.

    But I cannot seem to be able to capture the role a member has in a committee. The options seems to make links only between two tables... I tried many ways to add the Role table to it but was unsuccessful on my own.

    I did a lot of searching but to no avail. If someone can give me any insight, it would be greatly appreciated.

    Thank you in advance for your replies.


    Code: the model builder I have that works partially is the following:
    Code:
        modelBuilder.Entity<Committee>()
            .HasMany(e => e.Members)
            .WithMany(e => e.Committees)
            .Map(m => m.ToTable("LNK_Member_Committee_Role")
            .MapLeftKey("CommitteeId")
            .MapRightKey("MemberId"));
    
        public partial class LNK_Member_Committee_Role
        {
            public int Id { get; set; }
            public int MemberId { get; set; }
            public int CommitteeId { get; set; }
            public int RoleId { get; set; }
        }
    
    
        public partial class Committee
        {
            public Committee()
            {
                Members = new HashSet<Member>();
                Roles = new HashSet<Role>();
                ChildCommittees = new HashSet<Committee>();
            }
            public int Id { get; set; }
            public int? ParentCommitteeId { get; set; }
            public string DescriptionE { get; set; }
            public string DescriptionF { get; set; }
            public string Abbreviation { get; set; }
    
            public virtual ICollection<Role> Roles { get; set; }
            public virtual ICollection<Member> Members { get; set; }
            public virtual Committee ParentCommittee { get; set; }
            public virtual ICollection<Committee> ChildCommittees { get; set; }
        }
    
        public partial class Member
        {
            public Member()
            {
                Committees = new HashSet<Committee>();
                Roles = new HashSet<Role>();
                Member_Items = new HashSet<Member_Item>();
            }
            [Key]
            public int Id { get; set; }
            public int? DistrictId { get; set; }
            public int LanguageId { get; set; }
            public int TitleId { get; set; }
            public int PartyId { get; set; }
            public int GenderId { get; set; }
            public string Given { get; set; }        
            public string Family { get; set; }
            public string Image_Path { get; set; }
            
            public virtual Language Language { get; set; }
            public virtual Title Title { get; set; }
            public virtual Party Party { get; set; }
            public virtual District District { get; set; }
            public virtual Gender Gender { get; set; }
    
            public virtual Role Role { get; set; }
            public virtual Committee Committee { get; set; }
    
            public virtual ICollection<Member_Item> Member_Items { get; set; }
            public virtual ICollection<Committee> Committees { get; set; }
            public virtual ICollection<Role> Roles { get; set; }
        }
    
        public partial class Role
        {
            public Role()
            {
                Committees = new HashSet<Committee>();
                Members = new HashSet<Member>();
            }
            [Key]
            public int Id { get; set; }
            public string DescriptionE { get; set; }
            public string DescriptionF { get; set; }
    
            public virtual ICollection<Committee> Committees { get; set; }
            public virtual ICollection<Member> Members { get; set; }
    
            public virtual Member Member { get; set; }
            public virtual Committee Committee { get; set; }
        }
Working...