Search for existing suggestions

Extra Lazy Loading

When I do user.Cars.Add() EF load all Cars for that user.
user.Cars.Count() too

78 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

Felipe Fujiy Pessoto shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Kieron Dye commented  ·   ·  Flag as inappropriate

    Two questions:

    1) Does EntityCollection<T> solve this issue when calling Add()?
    2) How can we unit test code that uses EntityCollection<T>.CreateSourceQuery and has no entity context?

  • shawn commented  ·   ·  Flag as inappropriate

    It would also be nice if you could filter a relationship without materializing the entire collection. For example:

    category.Products.Where(p => p.Brand == "foo");
    category.Products.Where(p => p.IsActive);

    Having to materialize every item just to retrieve/count a small subset of them can be a huge unnecessary performance hit. If the collection isn't already loaded in memory, it would be great to at least have the option of running the query in the database rather than materializing a bunch of data that isn't being used.

  • Felipe Fujiy Pessoto commented  ·   ·  Flag as inappropriate


    I am using Code First, then I need to do this:
    Post post = db.Posts.First();

    db.Entry(post).Collection(x => x.Categories).Query().Count();

    My request is for something like Ayende and Arthur Vickers(MS EF Developer) talk about:

  • Ladislav Mrnka commented  ·   ·  Flag as inappropriate

    This is already implemented you just need to call user.Cars.CreateSourceQuery().Count() where Cars must be EntityCollection<Car>.

  • Jeremy commented  ·   ·  Flag as inappropriate

    This would be a nice optional feature to have. In my opinion, EF should not have to load all the cars that belong to the user just to add a new car. The same goes for counting the number of cars that a user owns. EF should not have to load all the cars to get a count, but instead, execute a count query and then return the value.

  • akns commented  ·   ·  Flag as inappropriate

    wouldn't that more accurately called "Extra eager loading"?

Feedback and Knowledge Base