I am trying to update the performance of my code regarding database queries. The problem I'm currently facing is that I cannot find a way to get a new context for each subQuery.
Using the simplified code below, you arbitrarily generate an "Invalid base provider upon opening".
using (var context = getNewContextObject()) { var result = new SomeResultObject(); var parentQuery = context.SomeTable.Where(x => x.Name = "asdf"); Parallel.Invoke(() => { result.count1 = parentQuery.Where(x => x.Amount >= 100 & x.Amount < 2000).Count(); }, () => { result.count2 = parentQuery.Where(x => x.Amount < 100).Count(); } , () => { result.count3 = parentQuery.Where(x => x.Amount >= 2000).Count(); } ); }
The only way it still seems to be to rebuild the entire query for each subQuery with a new context. Is there a way to avoid building each request from the bottom up using the new Context? Can I instead simply attach each subquery request to a new context? I am looking for something like below.
Parallel.Invoke(() => { var subQuery = parentQuery.Where(x => x.Amount >= 100 & x.Amount < 2000).Count(); subQuery.Context = getNewContextObject(); result.count1 = subQuery.Count(); }, () => { var subQuery = parentQuery.Where(x => x.Amount < 100).Count(); subQuery.Context = getNewContextObject(); result.count2 = subQuery.Count(); } , () => { var subQuery = parentQuery.Where(x => x.Amount >= 2000).Count(); subQuery.Context = getNewContextObject(); result.count3 = subQuery.Count(); } ); }
source share