entity framework - load method
We use Load() method to explicitly load related data. Actually, the Entity Framework returns the data we have specified in the query. But it is possible to get the related data using the same query. For example, when we have written a query for retrieving customers, we can also get the sales order details without writing an another query. This is known as deferred loading or lazy loading. By default, the Entity Framework performs lazy loading.
Consider the following query.
using (var context = new SalesOrderManagementEntities()) { //1. Query all the customers using LINQ to Entities query using c#. var Customers = from c in context.Customer select c; foreach (var Customer in Customers) { Customer.Order.Load(); Console.WriteLine(String.Format("{0} {1} {2}", Customer.Name, Customer.Address, Customer.Order.Count())); } Console.WriteLine(); Console.ReadLine();
The output of the above query is:

In the above query, I have called Load() method. When Load is called, ObjectServices will execute a query to retrieve all of the orders of a particular customer.
Note that by default, for the models created in VS2008, lazy loading will not be enabled. So, when lazy loading is disabled, we need to explicitly call Load() method to load entity collections.
If models are created in VS2010, by default lazy loading is enabled, so, the Entity Framework retrieves all the related data using a single query without explicitly calling the Load() method.
using (var context = new SalesOrderManagementEntities()) { //1. Query all the customers using LINQ to Entities query using c#. var Customers = from c in context.Customer select c; foreach (var Customer in Customers) { Console.WriteLine(String.Format("{0} {1} {2}", Customer.Name, Customer.Address, Customer.Order.Count())); } Console.WriteLine(); Console.ReadLine();
Performance issue with Lazy loading in Entity Framework
Whether we use lazy loading default feature or call Load() method to explicitly load related data, performance will be hit. In the above code, a new query will be executed to count number of orders for each customer which results in an addition trip to the database for each customer. If the database contains 10 customers, there will be 20 additional round trips to count number of orders. This is not efficient, Load() can be used to filter orders only for particular customers and number of round trips to the database can be reduced.
Entity Framework Articles
- entity framework entity data model
- entity framework orm object relational mapper
- querying entity data model using objectcontext
- using ado net entity data model in windows applications.html
- create entity data model using ado.net entity framework designer in visual studio
- create-sales-order-management-data-model-using-edmgen-tool.html
- display data in a gridview using ado.net entity data source control
- ways of querying entity data model
- query entity data model using linq to entities
- query entity data model with object services and entity sql
- query entity data model using linq methods
- parameterized objectquery against a entity data model
- query entity data model entity client
- linq to entities query with projections in c#
- entity framework - save changes to entities
- entity framework - insert new objects
- add an entity to associated entities
- deleting entities using deleteobject() method of objectcontext
- eager loading using include method in entity framework
- entity framework - retrieving a single entity
Most Viewed
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