EF Core - Explicit Loading


What Is Explicit Loading in EF Core?

Explicit loading is a data retrieval strategy in EF Core where related entities are manually loaded by calling specific methods. This approach gives you precise control over when and how related data is loaded, making it useful for scenarios where neither eager loading nor lazy loading is appropriate.


Types of Loading Strategies in EF Core

EF Core provides several strategies for loading related data. The following table summarizes the main types:

Strategy Description Use Case
Eager Loading Loads related entities as part of the initial query. When you need related data immediately.
Lazy Loading Loads related entities on demand when accessed. When related data is not always needed.
Explicit Loading Loads related entities manually by calling methods. When you need precise control over data loading.

1. Introduction to Explicit Loading

Explicit loading in EF Core allows you to manually load related entities by using methods like `Load` or `Collection`. This strategy gives you control over when and how related data is loaded, making it ideal for scenarios where data loading needs to be deferred until explicitly required.

        
            
var order = _context.Orders.Find(1);
_context.Entry(order).Collection(o => o.OrderItems).Load();
        
    

This example introduces the concept of explicit loading and its importance in optimizing data retrieval in EF Core.


2. Using the Load Method

The `Load` method in EF Core is used to explicitly load related entities into the context, allowing you to load navigation properties as needed.

        
            
var customer = _context.Customers.Find(1);
_context.Entry(customer).Collection(c => c.Orders).Load();
        
    

This example demonstrates how to use the `Load` method to perform explicit loading in EF Core.


3. Loading Collections with Collection Method

The `Collection` method in EF Core allows you to explicitly load a collection navigation property for an entity, giving you precise control over which collections are loaded.

        
            
var order = _context.Orders.Find(1);
_context.Entry(order).Collection(o => o.OrderItems).Load();
        
    

This example shows how to use the `Collection` method to load collections explicitly in EF Core.


4. Loading Reference Properties with Reference Method

The `Reference` method in EF Core is used to explicitly load a reference navigation property for an entity, allowing you to load specific related entities on demand.

        
            
var order = _context.Orders.Find(1);
_context.Entry(order).Reference(o => o.Customer).Load();
        
    

This example illustrates how to use the `Reference` method to load reference properties explicitly in EF Core.


5. Filtering Data During Explicit Loading

You can apply filters to explicitly loaded data using LINQ queries, allowing you to load only the data that meets specific criteria.

        
            
var customer = _context.Customers.Find(1);
_context.Entry(customer)
    .Collection(c => c.Orders)
    .Query()
    .Where(o => o.TotalAmount > 100)
    .Load();
        
    

This example demonstrates how to filter data during explicit loading in EF Core.


6. Handling Circular References in Explicit Loading

Circular references can occur when entities reference each other in a loop. Explicit loading provides a way to handle circular references by controlling when related data is loaded.

        
            
var employee = _context.Employees.Find(1);
_context.Entry(employee).Reference(e => e.Manager).Load();
        
    

This example shows how to handle circular references with explicit loading in EF Core.


7. Performance Considerations with Explicit Loading

Explicit loading can optimize performance by loading only the data you need when you need it. Understanding how to use explicit loading effectively can help you avoid unnecessary data retrieval and improve application performance.

        
            
var orders = _context.Orders.Take(10).ToList();
foreach (var order in orders)
{
    _context.Entry(order).Collection(o => o.OrderItems).Load();
}
        
    

This example provides tips for optimizing performance when using explicit loading in EF Core.


8. Comparing Explicit Loading with Other Strategies

Comparing explicit loading with eager loading and lazy loading helps you understand the trade-offs between these strategies and choose the right approach for your application.

        
            
var customer = _context.Customers.Find(1);
// Eager Loading
var eagerOrders = _context.Customers
    .Include(c => c.Orders)
    .FirstOrDefault(c => c.CustomerId == 1);

// Explicit Loading
_context.Entry(customer).Collection(c => c.Orders).Load();
        
    

This example compares explicit loading with other data loading strategies in EF Core.


9. Testing Explicit Loading

Testing explicit loading involves verifying that related data is correctly loaded when requested and that performance is optimized. This section covers testing strategies for explicit loading.

        
            
var order = _context.Orders.Find(1);
_context.Entry(order).Collection(o => o.OrderItems).Load();
Assert.NotNull(order.OrderItems);
Assert.True(order.OrderItems.Count > 0);
        
    

This example demonstrates techniques for testing explicit loading in EF Core.


10. Configuring Explicit Loading in EF Core 8

EF Core 8 provides enhancements for configuring and using explicit loading, offering more options and flexibility for developers.

        
            
var product = _context.Products.Find(1);
_context.Entry(product).Collection(p => p.Reviews).Load();
        
    

This example highlights the enhancements for explicit loading in EF Core 8.


11. Advanced Techniques for Explicit Loading

Advanced techniques for explicit loading involve customizing the loading behavior and combining explicit loading with other strategies to optimize data retrieval.

        
            
var category = _context.Categories.Find(1);
_context.Entry(category)
    .Collection(c => c.Products)
    .Query()
    .Include(p => p.Supplier)
    .Load();
        
    

This example explores advanced techniques for using explicit loading in EF Core.


12. Best Practices for Explicit Loading

Following best practices when using explicit loading can help ensure efficient and maintainable code. Consider the following guidelines:


Summary

Explicit loading in EF Core is a powerful strategy for manually loading related entities when you need precise control over data retrieval. By understanding how to use explicit loading effectively and balancing it with other loading strategies, developers can build performant applications that efficiently interact with relational databases.