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

  • SEARCH
  • Home
  • 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 7193937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:16:12+00:00 2026-05-28T20:16:12+00:00

I’m using Entity Framework 4.1 code-first and I’m having a slight issue with retriving

  • 0

I’m using Entity Framework 4.1 code-first and I’m having a slight issue with retriving associated records in a many-to-many relationship. I have a main Member entity which then has an associated MemberStatus (one-to-many relationship) and an associated MemberPosition (many-to-many relationship).

For an admin console, I am retreiving a list of MemberStatuses or MemberPositions, but I need to know if there are members assigned to those values so that I can determine if the value can be deleted.

I have the following code that I am using in my admin console:

var statusList = MemberStatusRepository.AllIncluding(x => x.Members).ToList(); // This works...
var positionList = MemberPositionRepository.AllIncluding(x => x.Members).ToList(); // This doesn't...

The statusList value comes back as expected and the collection of statuses includes a count of how many members are assigned. However, the postionList comes back with the appropriate list, but the Members collection for each position is showing a count of 0 when I know there are members assigned.

What’s wierd is that I am able to get a list of positions for each member, so I belive the mapping is correct in my MemberMap. I just can’t get the reverse which is a list of members for each position. Also, the DB that is created by EF looks correct with the proper schema for a many-to-many relationship for Members to Positions.

The following is my code for the entities and the EF mapping (inheriting from EntityTypeConfiguration). What am I missing on the MemberPosition map so that when I call the above code, a list of members is returned with each position?

public class Member : Entity
{
  public string Email { get; set; }
  public int StatusId { get; set; }
  public virtual MemberStatus Status { get; set; }
  public virtual List<MemberPosition> Positions { get; set; }
}

public class MemberStatus : Entity
{
  public string Name { get; set; }
  public ICollection<Member> Members { get; set; }
}

public class MemberPosition : Entity
{
  public string Name { get; set; }
  public ICollection<Member> Members { get; set; }
}

public MemberMap()
{
  ToTable("Members");
  Property(m => m.Email).IsRequired().HasMaxLength(255);
  HasMany(m => m.Positions).WithMany() .Map(m => m.ToTable("Member_MemberPositions").MapLeftKey("MemberId").MapRightKey("PositionId"));
  HasRequired(m => m.Status).WithMany(s => s.Members).WillCascadeOnDelete(false);
}

public MemberStatusMap()
{
  ToTable("MemberStatuses");
}

public MemberPositionMap()
{
  ToTable("MemberPositions");
  HasMany(p => p.Members).WithMany();
}
  • 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-28T20:16:14+00:00Added an answer on May 28, 2026 at 8:16 pm

    In your many-to-many mapping the explicite specification of the inverse navigation property is missing. It should look like this:

     HasMany(m => m.Positions)
    .WithMany(p => p.Members)   // <- important to specify the navigation property
    .Map(m => m.ToTable("Member_MemberPositions")
               .MapLeftKey("MemberId")
               .MapRightKey("PositionId"));
    

    Your mapping will let EF create an additional one-to-many relationship between Member and MemberPosition. MemberPosition.Members is part of this relationship and not the many-to-many relationship. You should actually see this in the created database schema. I would expect a foreign key like MemberPosition_Id or similar in your Member table which belongs to this one-to-many relationship you don’t want to have. If that foreign key is NULL in the database (it should be a nullable column), your positions have no members. Therefore your second query doesn’t work as expected and returns empty member collections for all positions.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.