Explicit loading is a kind of lazy loading but the dependent
objects are not loaded automatically, but we will have to explicitly load the
dependent objects whenever needed. We have to use the Load() method to load related entities explicitly.
The initial data is loaded using a regular context query, the related data is loaded either using Reference for a single object or a Collection to load multiple related objects. Let us take the same Employee Department structure. First let us load the Employee details and later load the corresponding Department details using explicit loading.
Following are the LINQ queries to load employee and related department details using explicit loading.
var employee = _context.Employee
The initial data is loaded using a regular context query, the related data is loaded either using Reference for a single object or a Collection to load multiple related objects. Let us take the same Employee Department structure. First let us load the Employee details and later load the corresponding Department details using explicit loading.
Following are the LINQ queries to load employee and related department details using explicit loading.
var employee = _context.Employee
.Where(e =>
e.EmployeeId == 1)
.FirstOrDefault<Employee>();
When the first query executes the employee object contains all employee details and the Department property will be null.
Later when the second query executes, the department property in the employee object will be filled with the corresponding Department object as follows.
Using explicit loading we can delay the fetching of related object details to a later point when we actually need the data.
No comments:
Post a Comment