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
(thinking…)
Password icon
Signed in as (Sign out)

We’ll send you updates on this idea

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

6 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • 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

    Ladislav,

    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:

    http://ayende.com/blog/4351/nhibernate-vs-entity-framework-4-0

    http://blog.oneunicorn.com/2011/03/28/extra-lazy-collection-count-with-ef-4-1-part-2/

  • 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