I have an IQueryable extension method:
public static void SomeExt<T>(this IQueryable<T> query, DbContext context) {...}
and I would like to know if there is some way to get DbContext from query so that DbContext argument could be removed leaving only:
public static void SomeExt<T>(this IQueryable<T> query) {...}
I have tried something like this Access DataContext behind IQueryable but its not working, getting zero fields.
Also there is way to get it from DbSet
Can you get the DbContext from a DbSet?
myDbSet.GetService<'ICurrentDbContext>().Context;
but that's not what I need. I want to get it from Query?
This is the query:
var q = context.Items.Where(a => a.StatusId = 1);
q.SomeExt(context);
vs
q.SomeExt();
I have found a way to do this
public static DbContext GetDbContext(IQueryable query)
{
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
var queryCompiler = typeof(EntityQueryProvider).GetField("_queryCompiler", bindingFlags).GetValue(query.Provider);
var queryContextFactory = queryCompiler.GetType().GetField("_queryContextFactory", bindingFlags).GetValue(queryCompiler);
var dependencies = typeof(RelationalQueryContextFactory).GetProperty("Dependencies", bindingFlags).GetValue(queryContextFactory);
var queryContextDependencies = typeof(DbContext).Assembly.GetType(typeof(QueryContextDependencies).FullName);
var stateManagerProperty = queryContextDependencies.GetProperty("StateManager", bindingFlags | BindingFlags.Public).GetValue(dependencies);
var stateManager = (IStateManager)stateManagerProperty;
return stateManager.Context;
}
For EFCore 3 instead of
.GetProperty("Dependencies", bindingFlags)
use
.GetField("_dependencies", bindingFlags)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With