30

When creating a report I have to execute 3 queries that involve separated entities of the same context. Because they are quite heavy ones I decided to use the .ToListAsync(); in order to have them run in parallel, but, to my surprise, I get a exception out of it...

What is the correct way to perform queries in parallel using EF 6? Should I manually start new Tasks?

Edit 1
The code is basically

using(var MyCtx = new MyCtx())
{
      var r1 = MyCtx.E1.Where(bla bla bla).ToListAsync();
      var r2 = MyCtx.E2.Where(ble ble ble).ToListAsync();
      var r3 = MyCtx.E3.Where(ble ble ble).ToListAsync();
      Task.WhenAll(r1,r2,r3);
      DoSomething(r1.Result, r2.Result, r3.Result);
}
7
  • Possible duplicate of EF Data Context - Async/Await & Multithreading. Jan 19, 2017 at 19:20
  • A little tip. don't forget to call AsNoQueryable on your linq query. It will help with performance Jan 19, 2017 at 19:58
  • What exception do you get? can you share some code how you execute the queries in parallel?
    – Peter Bons
    Jan 19, 2017 at 20:47
  • 1
    @MiltonFilho "AsNoQueryable"?!? that sounds highly doubtful... and google has no knowledge about that method... is that from a framework?
    – Leonardo
    Jan 19, 2017 at 22:16
  • 3
    Sorry the correct is AsNoTracking. It will tell to EF don't observe all properties from all objects and will run fast. Jan 19, 2017 at 22:52

3 Answers 3

42

The problem is this:

EF doesn't support processing multiple requests through the same DbContext object. If your second asynchronous request on the same DbContext instance starts before the first request finishes (and that's the whole point), you'll get an error message that your request is processing against an open DataReader.

Source: https://visualstudiomagazine.com/articles/2014/04/01/async-processing.aspx

You will need to modify your code to something like this:

async Task<List<E1Entity>> GetE1Data()
{
    using(var MyCtx = new MyCtx())
    {
         return await MyCtx.E1.Where(bla bla bla).ToListAsync();
    }
}

async Task<List<E2Entity>> GetE2Data()
{
    using(var MyCtx = new MyCtx())
    {
         return await MyCtx.E2.Where(bla bla bla).ToListAsync();
    }
}

async Task DoSomething()
{
    var t1 = GetE1Data();
    var t2 = GetE2Data();
    await Task.WhenAll(t1,t2);
    DoSomething(t1.Result, t2.Result);
}
7
  • 3
    Are there worthwhile performance gains when doing something like this?
    – Josh
    Dec 10, 2018 at 15:25
  • 7
    @Josh it depends. If multiple calls (let's say 10 calls) each take 1 sec than parallel execution can lead to shorter total duration that 10 seconds when run sequential. But a database engine has its limits as well. If you bombard it with parallel requests its resource consumption will go up.
    – Peter Bons
    Dec 10, 2018 at 15:36
  • What if you want to have the loaded entities be all a part of a unit of work and make updates to them and commit them together in the end?
    – binki
    Sep 12, 2019 at 14:48
  • 2
    @mko you are probably relying on lazy loading, so EF tries to load additional data which fails due to the fact the context is disposed.
    – Peter Bons
    Sep 26, 2019 at 11:39
  • 3
    @binki: you could separate the reads from the writes: read the entities, detach them from the context. Then modify them and attach them to a single context and save the changes.
    – Peter Bons
    Sep 26, 2019 at 15:35
7

As a matter of interest, when using EF Core with Oracle, multiple parallel operations like the post here using a single DB context work without issue (despite Microsoft's documentation). The limitation is in the Microsoft.EntityFrameworkCore.SqlServer.dll driver, and is not a generalized EF issue. The corresponding Oracle.EntityFrameworkCore.dll driver doesn't have this limitation.

-3

Check out https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/enabling-multiple-active-result-sets

From the documentation:

Statement interleaving of SELECT and BULK INSERT statements is allowed. However, data manipulation language (DML) and data definition language (DDL) statements execute atomically.

Then your above code works and you get the performance benefits for reading data.

1
  • This doesn't answer the question. Mar 31, 2021 at 10:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.