Entity Framework Core 3.1 Load() Async Explicit Loading

1k Views Asked by At

I have alot of EF Explicit Calls in my code that load data after to avoid EF Cartesian Products (as it slows my queries to a standstill)

var users = await dbContext.User.ToListAsync();
foreach (var user in users )
{
   await dbContext.Entry(user).Reference(b => b.VirtualProperty1).LoadAsync();
   await dbContext.Entry(user).Reference(b => b.VirtualProperty2).LoadAsync();
   await dbContext.Entry(user).Collection(b => b.VirtualCollection).LoadAsync();
}

Question is, can I drop await and do await Task.WhenAll(AllAboveEagerLoadTasks) ?

1

There are 1 best solutions below

0
On

As Gert mentioned in a comment and I've tested myself, the context is not thread safe, so can't laod entities in parallel unfortunately. EF5 does offer a solution to SplitQueries.