Querying Entity Framework Entities: Eager Loading and Lazy Loading
In Entity Framework, querying related entities can be done using two techniques - Eager Loading and Lazy Loading. In this tutorial, we'll learn how to use both techniques to retrieve related entities.
Eager Loading
Eager loading is a technique in Entity Framework that allows loading related entities simultaneously along with the main entity. Eager loading is achieved by including related entities using the Include
method.
Syntax
db.Set<T>()
.Include(x => x.RelatedEntity)
.ToList()
Example
Suppose we have two entities, Department
and Employee
. Department
has a list of employees called Employees
.
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; }
}
To retrieve all departments and their related employees, we can use the following code:
var departments = _context.Set<Department>()
.Include(d => d.Employees)
.ToList();
Output
The above code will return a list of departments, and each department object will also contain its related employees.
Explanation
In the above syntax, we first retrieve the departments using db.Set<T>()
. Then, we include related entities using the Include()
method by passing a lambda expression, which maps to the related property. Finally, we call ToList()
to execute the query and return the entities.
Lazy Loading
Lazy loading is a technique in Entity Framework that delays the loading of related entities until they are explicitly accessed. Lazy loading is enabled by default in Entity Framework and can be disabled if required.
Syntax
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; } // Make the property virtual
}
var department = _context.Set<Department>().Find(id);
department.Employees.Load(); // Load related entities explicitly
Example
To retrieve a department and their related employees using lazy loading, we can use the following code:
var department = _context.Set<Department>().Find(id);
department.Employees.Load();
Output
The first line of the code will return a department object. When we access the Employees
property of the department object, the related entities will be loaded.
Explanation
In the above syntax, we first retrieve the department using db.Set<T>().Find(id)
. By default, the related entities are not loaded. When we access the Employees
property of the department object, the related employees will be loaded using lazy loading. We can also explicitly load related entities using Load()
method.
Use
Eager loading is useful when we need to load related entities along with the main entity, and we know in advance that we will access the related entities. On the other hand, lazy loading is useful when we don't need to load the related entities along with the main entity, and we want to minimize the data transfer.
Important Points
- Eager loading retrieves all related entities along with the main entity in a single query.
- Lazy loading retrieves related entities only when they are accessed explicitly.
- Lazy loading can cause performance issues if it results in multiple database trips.
Summary
In this tutorial, we learned about Eager Loading and Lazy Loading in Entity Framework. We've seen how to use both techniques to retrieve related entities efficiently. We've also seen when to use each of these techniques, and their advantages and disadvantages. Eager loading is used when related entities need to be loaded along with the main entity, and lazy loading is used to minimize data transfer and load related entities only when required.