Nabila Tajrin
0
Q:

how to run parallel queries in sql server with entity framework

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);
}
1
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.
0

New to Communities?

Join the community