Export (0) Print
Expand All

How to: Explicitly Load Related Objects

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic provides examples of how to explicitly load related objects.

The first example uses the Load method on EntityCollection to explicitly load all orders and items for a single customer. The Load method cannot be used with POCO entities because the navigation properties of POCO entities are not required to return EntityCollection. For more information, see Loading Related POCO Entities.

The second example uses the CreateSourceQuery method to create a query that loads only selected orders with the related items. These orders are then attached to the customer.

You could also use the LoadProperty method of the ObjectContext class to load related objects. The LoadProperty method can be used with POCO entities and with entities derived from EntityObject.

The examples in this topic are based on the Adventure Works Sales Model. For more information, see How to: Use the Entity Data Model Wizard.

Example

The following example loads SalesOrderHeader objects that belong to a single Contact and then iterates through the SalesOrderHeader objects in the EntityCollection. On each SalesOrderHeader object in the collection, the Load method is called to retrieve the collection of related SalesOrderDetail objects from the database.

The following example uses the CreateSourceQuery method on the EntityCollection to only load five SalesOrderHeader objects, with related SalesOrderDetail objects, that belong to a single Contact. This query result is then attached to the original SalesOrderHeader EntityCollection.

See Also

Show:
© 2014 Microsoft