Eager Loading and Lazy Loading in .NET Core

Introduction

In software development, efficient data retrieval is a critical aspect of creating high-performance applications. When working with databases, developers often face choices between loading strategies, two of the most common being eager loading and lazy loading. In the context of .NET Core, understanding the differences between eager loading and lazy loading is essential for optimizing data access and improving overall application performance.

What is Eager Loading?

Eager loading is a technique used to retrieve related or associated data in a single query from a database. It aims to minimize the number of database round-trips required to fetch data by retrieving all necessary information in one go. In .NET Core, eager loading is especially useful when dealing with complex data models involving multiple interconnected entities or tables.

Consider a scenario where we have two entities, such as an Author and a Book, where each Author can have multiple Book entries associated with them. Eager loading allows us to fetch both authors and their related books in a single query, optimizing performance and reducing unnecessary database hits.

Implementing Eager Loading

Let's explore the functionality of eager loading in Entity Framework Core. Assume we have the following entities defined in our .NET Core application.

public class Author
{
    public int AuthorId { get; set; }
    public string Name { get; set; }
    public ICollection<Book> Books { get; set; }
}

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}

Here, the Author class has a collection of Book objects, establishing a one-to-many relationship.

To perform eager loading using EF Core, we can use the Include method to specify related entities that should be fetched along with the main entity. Here's an example of eager loading in action.

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;

public class MyDbContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("YourConnectionString");
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var context = new MyDbContext())
        {
            var authorsWithBooks = context.Authors
                .Include(author => author.Books)
                .ToList();

            foreach (var author in authorsWithBooks)
            {
                Console.WriteLine($"Author: {author.Name}");

                foreach (var book in author.Books)
                {
                    Console.WriteLine($"- Book: {book.Title}");
                }
            }
        }
    }
}

In this example, Include(author => author.Books) within the query indicates that when fetching authors, EF Core should also load their associated books. This ensures that when authorsWithBooks is queried, the related books for each author are fetched in a single database query.

What is Lazy Loading?

Lazy loading is used to enhance performance by deferring the loading of certain resources or data until they are specifically needed. This approach optimizes application speed and memory usage by loading only the essential components required for the current operation rather than loading all resources upfront.

Example of Lazy Loading

Suppose we have a database context class AppDbContext and two entities: Author and Book. The Book entity has a navigation property referencing the Author.

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}

public class AppDbContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    // Other configurations...
}

In this scenario, if we retrieve a list of books, we might not necessarily need the corresponding author information immediately. Lazy loading allows us to load the Author entity only when it's accessed explicitly.

using (var context = new AppDbContext())
{
    var books = context.Books.ToList(); // Retrieves list of books

    foreach (var book in books)
    {
        Console.WriteLine($"Book Title: {book.Title}");
        Console.WriteLine($"Author: {book.Author.Name}"); // Lazy loading
    }
}

In the above code, when accessing the book.Author.Name, Entity Framework Core will lazily load the Author entity associated with each book. It triggers a separate database query only when accessing the Author property.

Enabling Lazy Loading in Entity Framework Core

Make sure you have the NuGet packages installed: Install-Package Microsoft.EntityFrameworkCore.Proxies.

By default, lazy loading is not enabled in Entity Framework Core to prevent unexpected performance hits caused by unintentional lazy loading of entire object graphs. To enable lazy loading explicitly, we can use the UseLazyLoadingProxies method in the DbContext configuration by overriding the OnConfiguring method or by configuring it in the Program.cs if you're using dependency injection.

Option 1. Override the OnConfiguring method in DbContext.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseLazyLoadingProxies();
}

Option 2: Using Dependency Injection in Program.cs:

builder.Services.AddDbContext<YourDbContext>(options =>
{
    options.UseLazyLoadingProxies();
    options.UseSqlServer(builder.Configuration.GetConnectionString("YourConnectionString"));
});

Key Differences Between Eager Loading and Lazy Loading

  • Timing of Data Retrieval
    • Eager loading fetches all related data in a single query upfront.
    • Lazy loading defers the loading of related entities until they are explicitly accessed.
  • Efficiency and Performance
    • Eager loading can be more efficient when retrieving multiple entities with their related data in a single query, reducing subsequent database hits.
    • Lazy loading might incur additional queries during runtime as related entities are fetched on demand, potentially affecting performance if not used judiciously.
  • Usage and Control
    • Eager loading provides more control over the data to be retrieved initially.
    • Lazy loading offers convenience but may lead to unexpected performance issues if overused or mismanaged.

Choosing the Right Approach

The choice between eager and lazy loading depends on specific use cases and performance considerations. Eager loading is suitable when related data is consistently required together, optimizing performance by reducing subsequent queries. On the other hand, lazy loading suits scenarios where minimizing initial data retrieval is critical or when associated data is not always needed.

Conclusion

Comprehending the differences between eager and lazy loading in .NET Core holds significant importance in refining data retrieval strategies. These methodologies present unique benefits and compromises, granting developers the flexibility to customize data retrieval mechanisms based on specific application needs and fine-tuning performance for optimal functionality.

I hope this article will give a clear understanding of eager and lazy loading in .NET Core to the readers. Happy Coding !!!


Similar Articles