Simplify Your Queries With "AutoInclude" In .Net Core
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.
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.