I'm trying to write a query provider that instantiates a DbContext
before execution, executes the query and then immediately disposes the generated context object.
For this I have to implement IQueryProvider.Execute(....)
. Inside of this method i need to get the IQueryProvider
from my DbContext
object to have it execute the query.
The problem is that I don't know how to access the provider from the context object. As far as I can see, it is stored in ((IObjectContextAdapter)context).ObjectContext.QueryProvider
but that property is internal.
I also tried getting it from a DbSet, but I don't know what type to specify.
context.Set(type?).AsQueryable().Provider.Execute<TResult>(expression)
or
context.Set<type?>().AsQueryable().Provider.Execute<TResult>(expression)
So my question is how to get the underlying IQueryProvider
from a given DbContext
object.
The underlying problem that I am trying to solve
I want to execute multiple queries in different contexts against the same database. However DbContext is IDisposable you have to wrap it in using.Blocks.
IEnumerable<T1> result1;
IEnumerable<T2> result2;
using(var context1 = new MyContext())
using(var context2 = new MyContext())
{
var result1Task = context1.Set<T1>().ToListAsync();
var result2Task = context2.Set<T2>().ToListAsync();
await Task.WhenAll(result1Task, result2Task).ConfigureAwait(false);
result1 = result1Task.Result;
result2 = result1Task.Result;
}
The alternative is to wrap it in own methods:
// could also be written as generic method GetResultAsync<T>(),
// but you should get the point :)
async Task<IEnumerable<T1>> GetResult1Async()
{
using(var context = new MyContext())
return await context.Set<T1>().ToListAsync().ConfigureAwait(false);
}
async Task<IEnumerable<T2>> GetResult2Async()
{
using(var context = new MyContext())
return await context.Set<T2>().ToListAsync().ConfigureAwait(false);
}
var result1Task = GetResult1Async();
var result2Task = GetResult2Async();
await Task.WhenAll(result1Task, result2Task).ConfigureAwait(false);
var result1 = result1Task.Result;
var result2 = result1Task.Result;
Both have a lot of boilerplate code, so I want to write a generalization for opening/disposing the context object.
I want to simplify this to something like this:
var result1Task = repository.Set<T1>().ToListAsync();
var result2Task = repository.Set<T2>().ToListAsync();
await Task.WhenAll(result1Task, result2Task).ConfigureAwait(false);
var result1 = result1Task.Result;
var result2 = result1Task.Result;
The trick is that I want repository.Set<T>()
to return IQueryable<T>
so i can use all Linq methods. For this purpose I am trying to creaty my own IQueryProvider
that wraps the context using-Block in its Execute method.
This is my class so far:
public class RepositoryQueryProvider<TContext> : IQueryProvider
where TContext : DbContext, new()
{
// some irrelevant code here
public TResult Execute<TResult>(Expression expression)
{
using (var context = new TContext())
{
var efProvider = ?; // how to get the provider from the context?
return efProvider.Execute<TResult>(expression);
}
}
}