how to run parallel queries in sql server with entity framework

Solutions on MaxInterview for how to run parallel queries in sql server with entity framework by the best coders in the world

showing results for - "how to run parallel queries in sql server with entity framework"
Emmy
20 Jul 2017
1async Task<List<E1Entity>> GetE1Data()
2{
3    using(var MyCtx = new MyCtx())
4    {
5         return await MyCtx.E1.Where(bla bla bla).ToListAsync();
6    }
7}
8
9async Task<List<E2Entity>> GetE2Data()
10{
11    using(var MyCtx = new MyCtx())
12    {
13         return await MyCtx.E2.Where(bla bla bla).ToListAsync();
14    }
15}
16
17async Task DoSomething()
18{
19    var t1 = GetE1Data();
20    var t2 = GetE2Data();
21    await Task.WhenAll(t1,t2);
22    DoSomething(t1.Result, t2.Result);
23}
Oskar
03 Jan 2018
1EF doesn't support processing multiple requests through the same DbContext object.
2If your second asynchronous request on the same DbContext instance starts before the first request finishes (and that's the whole point),
3you'll get an error message that your request is processing against an open DataReader.