Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6340875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:55:04+00:00 2026-05-24T19:55:04+00:00

I have this scenario: public class Member { public int MemberID { get; set;

  • 0

I have this scenario:

public class Member
{
    public int MemberID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

public class Comment
{
    public int CommentID { get; set; }
    public string Message { get; set; }

    public virtual ICollection<Member> Members { get; set; }
}

public class MemberComment
{
    public int MemberID { get; set; }
    public int CommentID { get; set; }
    public int Something { get; set; }
    public string SomethingElse { get; set; }
}

How do I configure my association with fluent API? Or is there a better way to create the association table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:55:04+00:00Added an answer on May 24, 2026 at 7:55 pm

    It’s not possible to create a many-to-many relationship with a customized join table. In a many-to-many relationship EF manages the join table internally and hidden. It’s a table without an Entity class in your model. To work with such a join table with additional properties you will have to create actually two one-to-many relationships. It could look like this:

    public class Member
    {
        public int MemberID { get; set; }
    
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        public virtual ICollection<MemberComment> MemberComments { get; set; }
    }
    
    public class Comment
    {
        public int CommentID { get; set; }
        public string Message { get; set; }
    
        public virtual ICollection<MemberComment> MemberComments { get; set; }
    }
    
    public class MemberComment
    {
        [Key, Column(Order = 0)]
        public int MemberID { get; set; }
        [Key, Column(Order = 1)]
        public int CommentID { get; set; }
    
        public virtual Member Member { get; set; }
        public virtual Comment Comment { get; set; }
    
        public int Something { get; set; }
        public string SomethingElse { get; set; }
    }
    

    If you now want to find all comments of members with LastName = “Smith” for example you can write a query like this:

    var commentsOfMembers = context.Members
        .Where(m => m.LastName == "Smith")
        .SelectMany(m => m.MemberComments.Select(mc => mc.Comment))
        .ToList();
    

    … or …

    var commentsOfMembers = context.MemberComments
        .Where(mc => mc.Member.LastName == "Smith")
        .Select(mc => mc.Comment)
        .ToList();
    

    Or to create a list of members with name “Smith” (we assume there is more than one) along with their comments you can use a projection:

    var membersWithComments = context.Members
        .Where(m => m.LastName == "Smith")
        .Select(m => new
        {
            Member = m,
            Comments = m.MemberComments.Select(mc => mc.Comment)
        })
        .ToList();
    

    If you want to find all comments of a member with MemberId = 1:

    var commentsOfMember = context.MemberComments
        .Where(mc => mc.MemberId == 1)
        .Select(mc => mc.Comment)
        .ToList();
    

    Now you can also filter by the properties in your join table (which would not be possible in a many-to-many relationship), for example: Filter all comments of member 1 which have a 99 in property Something:

    var filteredCommentsOfMember = context.MemberComments
        .Where(mc => mc.MemberId == 1 && mc.Something == 99)
        .Select(mc => mc.Comment)
        .ToList();
    

    Because of lazy loading things might become easier. If you have a loaded Member you should be able to get the comments without an explicit query:

    var commentsOfMember = member.MemberComments.Select(mc => mc.Comment);
    

    I guess that lazy loading will fetch the comments automatically behind the scenes.

    Edit

    Just for fun a few examples more how to add entities and relationships and how to delete them in this model:

    1) Create one member and two comments of this member:

    var member1 = new Member { FirstName = "Pete" };
    var comment1 = new Comment { Message = "Good morning!" };
    var comment2 = new Comment { Message = "Good evening!" };
    var memberComment1 = new MemberComment { Member = member1, Comment = comment1,
                                             Something = 101 };
    var memberComment2 = new MemberComment { Member = member1, Comment = comment2,
                                             Something = 102 };
    
    context.MemberComments.Add(memberComment1); // will also add member1 and comment1
    context.MemberComments.Add(memberComment2); // will also add comment2
    
    context.SaveChanges();
    

    2) Add a third comment of member1:

    var member1 = context.Members.Where(m => m.FirstName == "Pete")
        .SingleOrDefault();
    if (member1 != null)
    {
        var comment3 = new Comment { Message = "Good night!" };
        var memberComment3 = new MemberComment { Member = member1,
                                                 Comment = comment3,
                                                 Something = 103 };
    
        context.MemberComments.Add(memberComment3); // will also add comment3
        context.SaveChanges();
    }
    

    3) Create new member and relate it to the existing comment2:

    var comment2 = context.Comments.Where(c => c.Message == "Good evening!")
        .SingleOrDefault();
    if (comment2 != null)
    {
        var member2 = new Member { FirstName = "Paul" };
        var memberComment4 = new MemberComment { Member = member2,
                                                 Comment = comment2,
                                                 Something = 201 };
    
        context.MemberComments.Add(memberComment4);
        context.SaveChanges();
    }
    

    4) Create relationship between existing member2 and comment3:

    var member2 = context.Members.Where(m => m.FirstName == "Paul")
        .SingleOrDefault();
    var comment3 = context.Comments.Where(c => c.Message == "Good night!")
        .SingleOrDefault();
    if (member2 != null && comment3 != null)
    {
        var memberComment5 = new MemberComment { Member = member2,
                                                 Comment = comment3,
                                                 Something = 202 };
    
        context.MemberComments.Add(memberComment5);
        context.SaveChanges();
    }
    

    5) Delete this relationship again:

    var memberComment5 = context.MemberComments
        .Where(mc => mc.Member.FirstName == "Paul"
            && mc.Comment.Message == "Good night!")
        .SingleOrDefault();
    if (memberComment5 != null)
    {
        context.MemberComments.Remove(memberComment5);
        context.SaveChanges();
    }
    

    6) Delete member1 and all its relationships to the comments:

    var member1 = context.Members.Where(m => m.FirstName == "Pete")
        .SingleOrDefault();
    if (member1 != null)
    {
        context.Members.Remove(member1);
        context.SaveChanges();
    }
    

    This deletes the relationships in MemberComments too because the one-to-many relationships between Member and MemberComments and between Comment and MemberComments are setup with cascading delete by convention. And this is the case because MemberId and CommentId in MemberComment are detected as foreign key properties for the Member and Comment navigation properties and since the FK properties are of type non-nullable int the relationship is required which finally causes the cascading-delete-setup. Makes sense in this model, I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this scenario: class A { public virtual int Id { get; set;
Have this scenario: public class Base { public string Name; } public Class ClassA
I have the following scenario: public class Login { public virtual int Id {
I have this following scenario namespace A { //... class A { public: static
I have 2 scenarios. This fails: class F<X> { public X X { get;
So I have this two classes: public class PhysicalTest { public int ID {
HI all, my scenario public class Permission { public virtual Function Function { get;
Lets say i have the following scenario: public class A { public String createString(final
I have common parent\children scenario: public class Order : AdvancedBaseOrder { ICollection<ProducerRelation> producers =
This is my scenario. I have a generic class: public class Tuple<T> extends ArrayList<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.