How to load related POCO Entities

In a Sales Order Management system example, entities such as customers and sales orders are related to each other. When creating POCO classes, we create the relationships between them. At runtime, when query is executed entities are generated and these entities are POCO-entities.

In the article loading related EntityObjects in Entity Framework, we have discussed how to load related entities using three patterns. The three patterns are - LazyLoading using navigation property without writing a query, ExplicitLoading using Load method and EagerLoading using Include method.
In this article, we will discuss how to load related entities (POCOs) using navigation properties. Lazyloading is not possible by default and we need to do some work to make it possible. This is because navigation properties that return collections are not EntityCollections. For this reason, explicitloading is also not possible using Load() method. So, how do we load related data with POCO's? This process is little bit different, even as we use the same three patterns we use with EF mentioned above. We will discuss each pattern separately. Depending on the requirement we choose any one of the below patterns and understanding all of them is necessary. Note that many a times we may have to choose more than one pattern to satisfy a requirement.

  • Explicit
  • Eager
  • Lazy

Explicit Loading
Explicit-Loading means we load related data explicitly. It is possible with POCOs because of the newly added functionalities to the ObjectContext in .Net 4. In .Net4, ObjectContext has a new method called LoadProperty and this is used to explicitly load data. Instead of calling Load method on a navigation property, we call LoadProperty of the ObjectContext to perform explicit loading with POCOs as shown in the following code. Note that the LoadProperty can be used with these classes or with EntityObject classes.

LoadProperty syntax code

In the above code, the Customer is the entity class, customer is the entity instance and c => c.SalesOrders is the lambda expression where c is the control variable followed by => (the lamda) and then the expression.

The code uses SOM model which contains Customer and SalesOrder entities with a one-to-many relationship between Customer and SalesOrder entities. We explicitly request sales orders data with LoadProperty method. After calling LoadProperty, the related objects can be accessed through the navigation properties and objects are loaded into the ObjectContext.

example code for retrieving data with LoadProperty method

The output of the code is shown below.

screenshot of LoadProperty method output

The advantage of using Explicit compared to Lazy Loading is that we can have more control over related entities to be returned.

Eager Loading
As with EF generated entities, we use the Include method to eagerly load related data. In the Include method, we specify the query path to load related POCO entities. The advantage of using this type of loading is that we can get all the related data from the database with one query.

Eager Loading Example code
Eager Loading Example code

Lazy Loading
As discussed, eager loading gets all the related data that means all the customers and all the sales orders. With lazy loading you can display sales orders for some customers or for a particular customer. If we want to get Lazy Loading capability, we need to declare the navigation property as virtual and also the collection which implements ICollection<T> interface. EF creates proxies at runtime. The runtime proxies give the capability to perform lazy loading. The article How to perform Lazy Loading/deferred Loading gives detailed explanation on this topic. The below code displays sales orders for selected customers.

Lazy Loading Example code