This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Virtual Keyword


Lazy loading in Entity Framework relies on the virtual keyword to function properly when using navigation properties. Here’s a more detailed explanation:

How Lazy Loading Works

Lazy loading in Entity Framework is achieved using proxy classes that inherit from your entity classes. These proxies are created at runtime and are responsible for intercepting calls to navigation properties to load related data only when it is accessed.

Role of the virtual Keyword

  • Virtual - When you mark a navigation property with virtual, Entity Framework can create a proxy that overrides this property. The proxy contains the logic needed to load related data from the database on demand.

Example Without virtual

If you don’t use the virtual keyword, Entity Framework won’t be able to create a proxy for your entity class. As a result:

  • Lazy Loading - Lazy loading won’t be available. The related data won’t be loaded automatically when you access the navigation property. You would need to use eager loading or explicitly load related data.

Example Without virtual

Let’s look at an example without lazy loading.

public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public ICollection<Employee> Employees { get; set; } // No lazy loading
}

In this case, accessing Employees won’t trigger a lazy load. You’d have to load the employees explicitly using Include or another method.

using (var context = new MyDbContext())
{
    // Explicitly load the related data
    var department = context.Departments
                            .Include(d => d.Employees) // Eagerly load employees
                            .FirstOrDefault(d => d.DepartmentId == 1);

    var employees = department.Employees; // Already loaded
}

So, if you need lazy loading, using the virtual keyword for navigation properties is necessary. If you prefer or need to avoid lazy loading, you can use eager loading with Include or load data explicitly as needed.


Prev Next