Load Related Objects (Entity Framework)

This topic presents patterns that you can use to load related objects. Entity types can define navigation properties that represent associations in the Entity Data Model (EDM). You can use these properties to load objects that are related to the returned object by the defined association. The following patterns describe the ways in which you can load related objects:

Loading Pattern Description

Specified in the query

You can compose an Entity SQL or LINQ to Entities query that explicitly navigates relationships by using navigation properties. When you execute such a query, the related objects that you specified in the query are returned. For more information, see How to: Navigate Relationships Using Navigation Properties (Entity Framework).

Explicit loading

In this pattern, related objects are only loaded into the ObjectContext when you explicitly request them. You can use the Load method on an EntityCollection or EntityReference to explicitly retrieve the related objects from the data source. Each call to the Load method opens a connection to the database to retrieve the related information. This ensures that a query is never executed without an explicit request for the related object. Explicit loading is the default behavior of the Entity Framework.

NoteNote
Before Load is called, a small amount of information about the related object is already loaded into the ObjectContext.

For more information, see How to: Explicitly Load Related Objects (Entity Framework).

Deferred loading

In this type of loading, related objects are automatically loaded from the data source when you access a navigation property. This pattern is typically known as lazy loading. With this type of loading, be aware that each navigation property that you access results in a separate query executing against the data source if the object is not already in the ObjectContext. This loading functionality is not explicitly provided in this version of the Entity Framework. However, it is possible to implement deferred loading by using the Entity Framework. For more information, see the sample Transparent Lazy Loading for Entity Framework.

Eager loading

When you know the exact shape of the graph of related objects that your application requires, you can use the Include method on the ObjectContext to define a query path that controls which related objects to return as part of the initial query. When you define a query path, all related objects of the type that is defined in the path are loaded with each object that the query returns. For more information, see How to: Use Query Paths to Shape Results (Entity Framework).

Performance Considerations

When you choose a pattern for loading related objects, consider the behavior of each approach with regard to the number and timing of connections made to the data source versus the amount of data returned by and the complexity of using a single query. Eager loading returns all related objects together with the queried objects in a single query. This means that, while there is only one connection made to the data source, a larger amount of data is returned in the initial query. Also, query paths result in a more complex query because of the additional joins that are required in the query that is executed against the data source. Explicit and deferred loading enables you to postpone the request for related object data until that data is actually needed. This yields a less complex initial query that returns less total data. However, each successive loading of a related object makes a connection to the data source and executes a query. For more information, see Performance Considerations for Entity Framework Applications.

See Also

Other Resources

Application Scenarios (Entity Framework)