
If you know you need related data for every entity retrieved, eager loading often offers the best performance, because a single query sent to the database is typically more efficient than separate queries for each entity retrieved. (In the following example, if you wanted to load the Administrator navigation property, you'd replace Collection(x => x.Courses) with Reference(x => x.Administrator).) Typically you'd use explicit loading only when you've turned lazy loading off.īecause they don't immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.

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.
#Ef select to new dto with icollections code
This 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 specify eager loading by using the Include method.Įxplicit loading. This typically results in a single join query that retrieves all of the data that's needed.

When the entity is read, related data is retrieved along with it. The DbContext class enables lazy loading by default.Įager loading. This results in multiple queries sent to the database - one for the entity itself and one each time that related data for the entity must be retrieved. However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. When the entity is first read, related data isn't retrieved. There are several ways that the Entity Framework can load related data into the navigation properties of an entity:
