AsSplitQuery for specified collection
Let's say we have an entity with multiple collection properties.
public class Asset
{
public string Name { get; set; }
public IList<string> Aliases { get; set; }
public IList<Address> Addresses { get; set; }
}
We can write a query like:
dbContext.Assets.Where(some_condition_here)
.AsSplitQuery()
.Include(x => x.Aliases)
.Include(x => x.Addresses)
.ToList()
As the result we will get entities with Aliases and Addresses and EF will generate three separate SQL queries behind the scene instead of om query with joins because we are using AsSplitQuery() method. It works as expected. I would like to have an option to split query only for specified properties like that:
dbContext.Assets.Where(some_condition_here)
.Include(x => x.Aliases, splitQuery: false)
.Include(x => x.Addresses, splitQuery: true)
.ToList()
In this case my expectation would be that EF generates just two SQL queries instead of three as in example above.
Having configuration on property level would be nice because we can reduce amount of separate SQL queries when it is not necessary. In my real world example we have 20-30 regular fields in entity and 5-10 collection properties. Not all of them are required to be separated, but just 2-3 collection properties.
@crimcol #29089 tracks specifying which navigations should always use split queries, and which should not. When we discussed that issue we considered whether this should be something that should be done per navigation on every query, as you are suggesting. We came to the conclusion that it would be better to specify it once in the model building, since the expected cardinality of the navigation is unlikely to change.