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 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.
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.
The output of the code is shown below.
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
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.
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.
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.
The output of the code is shown below.
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
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.
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- Rules to be followed while creating POCOs - 4 important rules
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- What is Change tracking in POCOs, ways of tracking
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Entity Framework EntityKey object
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- ObjectStateEntry class
- Entity state in Entity Framework - what, how, why?
- AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net