ASP .NET Tutorial: Loading Data (Eager, Explicit, Lazy)

beginner
16 min

ASP .NET Tutorial: Loading Data (Eager, Explicit, Lazy)

Welcome to this comprehensive guide on loading data in ASP .NET! This tutorial is designed to help both beginners and intermediate learners understand the concepts of Eager, Explicit, and Lazy loading in ASP .NET. Let's dive in!

Understanding Data Loading

Data loading in ASP .NET refers to the process of fetching data from a database and using it in our applications. There are three main strategies for data loading: Eager, Explicit, and Lazy loading.

Eager Loading (💡 Pro Tip:)

Eager loading is a technique where we fetch all the related data for an entity in a single database query. This is useful when we know that we will need the related data immediately.

csharp
using System.Linq; using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Product> Products { get; set; } public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } public IQueryable<Product> GetAllWithCategories() { return Products .Include(p => p.Category) // Eager loading the Category data .AsNoTracking(); } } public class Product { public int Id { get; set; } public string Name { get; set; } public int CategoryId { get; set; } public Category Category { get; set; } // Navigation property } public class Category { public int Id { get; set; } public string Name { get; set; } public ICollection<Product> Products { get; set; } // Collection navigation property }

In the above example, we are eagerly loading the Category data for all Products in a single query.

Explicit Loading (💡 Pro Tip:)

Explicit loading is used when we don't need the related data immediately but want to avoid hitting the database multiple times. We can use the .Load() method to explicitly load the related data.

csharp
using System.Linq; using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Product> Products { get; set; } public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } public IQueryable<Product> GetAll() { return Products; } public void LoadRelatedData(Product product) { product.Category.Load(); // Explicitly loading the Category data } } public class Product { public int Id { get; set; } public string Name { get; set; } public int CategoryId { get; set; } public Category Category { get; set; } // Navigation property } public class Category { public int Id { get; set; } public string Name { get; set; } public ICollection<Product> Products { get; set; } // Collection navigation property }

In the above example, we are fetching all the Products, and later, we are explicitly loading the Category data for a specific Product when needed.

Lazy Loading (💡 Pro Tip:)

Lazy loading is a technique where related data is loaded when it's first accessed. This is useful when we don't know whether we will need the related data or not.

csharp
using System.Linq; using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Product> Products { get; set; } public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.EnableSensitiveDataLogging(); } } public class Product { public int Id { get; set; } public string Name { get; set; } public int CategoryId { get; set; } public Category Category { get; set; } // Navigation property } public class Category { public int Id { get; set; } public string Name { get; set; } public ICollection<Product> Products { get; set; } // Collection navigation property }

In the above example, we are using lazy loading without explicitly enabling it. By default, EF Core uses lazy loading. However, it's recommended to explicitly enable it for better performance and security.

Quick Quiz
Question 1 of 1

What is Eager Loading in ASP .NET?

Quick Quiz
Question 1 of 1

How can you explicitly load related data in ASP .NET?

Quick Quiz
Question 1 of 1

What is Lazy Loading in ASP .NET?