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 5951861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:35:54+00:00 2026-05-22T17:35:54+00:00

I’ve been looking at Applying filters when explicitly loading related entities and could not

  • 0

I’ve been looking at Applying filters when explicitly loading related entities and could not get it to work for a many-to-many relationship.

I created a simple model:Model

Brief description:
A Student can take many Courses and a Course can have many Students.
A Student can make many Presentation, but a Presentation can be made by only one Student.
So what we have is a many-to-many relationship between Students and Courses, as well as a one-to-many relationship between Student and Presentations.

I’ve also added one Student, one Course and one Presentation related to each other.

Here is the code I am running:

class Program
{
    static void Main()
    {
        using (var context = new SportsModelContainer())
        {
            context.Configuration.LazyLoadingEnabled = false;
            context.Configuration.ProxyCreationEnabled = false;

            Student student = context.Students.Find(1);

            context.
                Entry(student).
                Collection(s => s.Presentations).
                Query().
                Where(p => p.Id == 1).
                Load(); 

            context.
                Entry(student).
                Collection(s => s.Courses).
                Query().
                Where(c => c.Id == 1).
                Load();

            // Trying to run Load without calling Query() first
            context.Entry(student).Collection(s => s.Courses).Load();
        }
    }
}

After loading the presentations I see that the count for Presentations changed from 0 to 1: After loading presentations. However, after doing the same with Courses nothing changes: After attempting to load courses

So I try to load the courses without calling Query and it works as expected: Courses loaded

(I removed the Where clause to further highlight the point – the last two loading attempts only differ by the “Query()” call)

Now, the only difference I see is that one relationship is one-to-many while the other one is many-to-many. Is this an EF bug, or am I missing something?

And btw, I checked the SQL calls for the last two Course-loading attempts, and they are 100% identical, so it seems that it’s EF that fails to populate the collection.

  • 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-22T17:35:55+00:00Added an answer on May 22, 2026 at 5:35 pm

    I could reproduce exactly the behaviour you describe. What I got working is this:

    context.Entry(student)
           .Collection(s => s.Courses)
           .Query()
           .Include(c => c.Students)
           .Where(c => c.Id == 1)
           .Load();
    

    I don’t know why we should be forced also to load the other side of the many-to-many relationship (Include(...)) when we only want to load one collection. For me it feels indeed like a bug unless I missed some hidden reason for this requirement which is documented somewhere or not.

    Edit

    Another result: Your original query (without Include) …

    context.Entry(student)
           .Collection(s => s.Courses)
           .Query()
           .Where(c => c.Id == 1)
           .Load();
    

    … actually loads the courses into the DbContext as …

    var localCollection = context.Courses.Local;
    

    … shows. The course with Id 1 is indeed in this collection which means: loaded into the context. But it’s not in the child collection of the student object.

    Edit 2

    Perhaps it is not a bug.

    First of all: We are using here two different versions of Load:

    DbCollectionEntry<TEntity, TElement>.Load()
    

    Intellisense says:

    Loads the collection of entities from
    the database. Note that entities that
    already exist in the context are not
    overwritten with values from the
    database.

    For the other version (extension method of IQueryable) …

    DbExtensions.Load(this IQueryable source);
    

    … Intellisense says:

    Enumerates the query such that for
    server queries such as those of
    System.Data.Entity.DbSet,
    System.Data.Objects.ObjectSet,
    System.Data.Objects.ObjectQuery,
    and others the results of the query
    will be loaded into the associated
    System.Data.Entity.DbContext,
    System.Data.Objects.ObjectContext or
    other cache on the client. This is
    equivalent to calling ToList and then
    throwing away the list without the
    overhead of actually creating the
    list.

    So, in this version it is not guaranteed that the child collection is populated, only that the objects are loaded into the context.

    The question remains: Why gets the Presentations collection populated but not the Courses collection. And I think the answer is: Because of Relationship Span.

    Relationship Span is a feature in EF which fixes automatically relationships between objects which are in the context or which are just loaded into the context. But this doesn’t happen for all types of relationships. It happens only if the multiplicity is 0 or 1 on one end.

    In our example it means: When we load the Presentations into the context (by our filtered explicit query), EF also loads the foreign key of the Presentation entites to the Student entity – “transparently”, which means, no matter if the FK is exposed as property in the model of not. This loaded FK allows EF to recognize that the loaded Presentations belong to the Student entity which is already in the context.

    But this is not the case for the Courses collection. A course does not have a foreign key to the Student entity. There is the many-to-many join-table in between. So, when we load the Courses EF does not recognize that those courses belong to the Student which is in the context, and therefore doesn’t fix the navigation collection in the Student entity.

    EF does this automatic fixup only for references (not collections) for performance reasons:

    To fix relationship, EF transparently
    rewrites the query to bring
    relationship info for all relations
    which has multiplicity of 0..1 or1 on
    the other end; in other words
    navigation properties that are entity
    reference. If an entity has
    relationship with multiplicity of
    greater then 1, EF will not bring back
    the relationship info because it could
    be performance hit and as compared to
    bringing a single foreign along with
    rest of the record. Bringing
    relationship info means retrieving all
    the foreign keys the records has.

    Quote from page 128 of Zeeshan Hirani’s in depth guide to EF.

    It is based on EF 4 and ObjectContext but I think this is still valid in EF 4.1 as DbContext is mainly a wrapper around ObjectContext.

    Unfortunately rather complex stuff to keep in mind when using Load.

    And another Edit

    So, what can we do when we want to explicitely load one filtered side of a many-to-many relationship? Perhaps only this:

    student.Courses = context.Entry(student)
           .Collection(s => s.Courses)
           .Query()
           .Where(c => c.Id == 1)
           .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.