2
votes

Eager Loading:

context.Cars.Include(c=>c.Orders)

Explicit Loading:

context.Entry(c).Collection(x => x.Orders).Load();

what's the differences between Eager Loading and Explicit Loading? is it just syntax differences like Eager Loading uses Include while Explicit Loading uses Load?, but isn't that using Include is also an "explicit" way to load navigation properties, so why Eager Loading is not considered the same as Explicit Loading?

2

2 Answers

2
votes

Eager loading is the opposite of Lazy loading, but Explicit loading is similar to lazy loading, except that: you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property.

You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load method for collections or the Reference.Load method for properties that hold a single entity.

EntityFramework returns IQueryable objects, which essentially contain the query to the database. But these are not executed until the first time they are enumerated.

Load() executes the query so that its results are stored locally. Calling Load() is the same as calling ToList() and throwing away that List, without having the overhead of creating the List.

1
votes

Eager loading loads related entities as part of the query, i.e. the enties are loaded when the query is actually executed. This is the opposite of lazy loading where the related entities are loaded when you access them through a navigation property.

Calling Load() explicitly loads the entities on your request instead of waiting for you to access the navigation properties. It's for example useful when the initial query doesn't return any related entities (because you didn't use eager loading) and you have disabled lazy loading for whatever reason.