Simplify Your Queries With “AutoInclude” In EntityFramework Core

Are you tired of writing repetitive Include() statements in every query? Entity Framework Core’s AutoInclude() feature might just be the solution you’ve been waiting for,

AutoInclude() allows you to configure navigation properties to be automatically included whenever the corresponding entity is loaded from the database. This eliminates the need to manually specify navigation properties with Include() for every query, making your code cleaner and more readable.

What is AutoInclude()?

In EF Core, navigation properties define relationships between entities. When loading entities, related data often needs to be included. Traditionally, this is done using Include() statements, which can become repetitive and clutter your code.

With AutoInclude(), you can configure navigation properties directly in your model, so they’re always included by default.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>()
                .Navigation(o => o.Customer)
                .AutoInclude();
}

Now, whenever an Order entity is loaded, its Customer navigation property is automatically included.

Benefits of Using AutoInclude()

  • Reduces Boilerplate Code: No more repetitive Include() statements in your LINQ queries.
  • Improves Readability: Query expressions become more concise and easier to understand.
  • Consistency: Ensures that specified navigation properties are consistently loaded across the application.

AutoInclude in Derived Types

The power of AutoInclude() extends to navigation properties configured in derived types. If a base entity has navigation properties set with AutoInclude(), those configurations are inherited by derived entities.

For example

modelBuilder.Entity<DerivedOrder>()
            .Navigation(o => o.SpecialDetails)
            .AutoInclude();

Whenever a DerivedOrder is queried, its SpecialDetails navigation property is included automatically.

Overriding AutoInclude() with IgnoreAutoIncludes()

There may be scenarios where you need to exclude navigation properties configured with AutoInclude() for specific queries. EF Core provides the IgnoreAutoIncludes() method for such cases:

var orders = context.Orders
                    .IgnoreAutoIncludes()
                    .ToList();

This query excludes all auto-included navigation properties, giving you precise control over what data is fetched.

Limitations of AutoInclude()

While AutoInclude() offers significant advantages, it does come with a notable limitation: it doesn’t support filters.

For example, with Include(), you can apply filters like this.

var orders = context.Orders
                    .Include(o => o.Items.Where(i => i.Quantity > 5))
                    .ToList();

However, AutoInclude() always fetches the entire navigation property without filtering. If you require filtered navigation properties, you’ll need to stick with Include().

When to Use AutoInclude()?

Use AutoInclude() when.

  • Navigation properties are frequently required across the application.
  • The fetched data doesn’t require additional filtering.
  • You want to reduce repetitive code and improve maintainability.

Avoid AutoInclude() if,

  • You need filtered navigation properties.
  • Including specific properties might cause unnecessary performance overhead.

Conclusion

AutoInclude() is a powerful feature that enhances EF Core’s usability by minimizing repetitive code and ensuring consistent data fetching. However, understanding its limitations and knowing when to override it with IgnoreAutoIncludes() is crucial for maintaining optimal performance and flexibility in your queries.

By leveraging AutoInclude(), you can write cleaner, more efficient, and more maintainable code. Give it a try in your EF Core projects and experience the difference.

Keep learning!


Similar Articles