Accessing internal DbContext with reflection
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
I have a DbContext
like that.:
Domain.MyDbContext
public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>
public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;
Some reusable queries like that:
Extenions.OcorrenciasTalao
public static class OcorrenciasTalao
private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());
public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)
var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);
Since I want to extend the DbSet<TEntity>
indeed of the DbContext
, I need to get the InternalDbContext
in order to compile
my reusable query.
Extenions.Common
public static class Common
private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));
return Common._fields[type].GetValue(dbSet) as MyDbContext;
So, is a better way to get the InternalDbContext
without rely on Reflection
, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?
As you may have noticed, I'm using Extensions
like a Repository Pattern
, is there a good idea? how i can improve that?
c# reflection extension-methods entity-framework-core
 |Â
show 1 more comment
up vote
-1
down vote
favorite
I have a DbContext
like that.:
Domain.MyDbContext
public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>
public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;
Some reusable queries like that:
Extenions.OcorrenciasTalao
public static class OcorrenciasTalao
private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());
public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)
var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);
Since I want to extend the DbSet<TEntity>
indeed of the DbContext
, I need to get the InternalDbContext
in order to compile
my reusable query.
Extenions.Common
public static class Common
private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));
return Common._fields[type].GetValue(dbSet) as MyDbContext;
So, is a better way to get the InternalDbContext
without rely on Reflection
, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?
As you may have noticed, I'm using Extensions
like a Repository Pattern
, is there a good idea? how i can improve that?
c# reflection extension-methods entity-framework-core
2
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
2
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58
 |Â
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a DbContext
like that.:
Domain.MyDbContext
public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>
public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;
Some reusable queries like that:
Extenions.OcorrenciasTalao
public static class OcorrenciasTalao
private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());
public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)
var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);
Since I want to extend the DbSet<TEntity>
indeed of the DbContext
, I need to get the InternalDbContext
in order to compile
my reusable query.
Extenions.Common
public static class Common
private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));
return Common._fields[type].GetValue(dbSet) as MyDbContext;
So, is a better way to get the InternalDbContext
without rely on Reflection
, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?
As you may have noticed, I'm using Extensions
like a Repository Pattern
, is there a good idea? how i can improve that?
c# reflection extension-methods entity-framework-core
I have a DbContext
like that.:
Domain.MyDbContext
public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>
public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;
Some reusable queries like that:
Extenions.OcorrenciasTalao
public static class OcorrenciasTalao
private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());
public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)
var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);
Since I want to extend the DbSet<TEntity>
indeed of the DbContext
, I need to get the InternalDbContext
in order to compile
my reusable query.
Extenions.Common
public static class Common
private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));
return Common._fields[type].GetValue(dbSet) as MyDbContext;
So, is a better way to get the InternalDbContext
without rely on Reflection
, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?
As you may have noticed, I'm using Extensions
like a Repository Pattern
, is there a good idea? how i can improve that?
c# reflection extension-methods entity-framework-core
edited Jul 17 at 16:07
t3chb0t
31.8k54095
31.8k54095
asked Jul 16 at 20:19
Tobias Mesquita
1021
1021
2
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
2
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58
 |Â
show 1 more comment
2
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
2
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58
2
2
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
2
2
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58
 |Â
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199624%2faccessing-internal-dbcontext-with-reflection%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Please post your real code. Psedocode is off-topic on Code Review.
â t3chb0t
Jul 17 at 6:26
@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
â Tobias Mesquita
Jul 17 at 12:00
2
The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
â Graipher
Jul 17 at 12:51
Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
â t3chb0t
Jul 17 at 18:57
Entity Framework Core
â Tobias Mesquita
Jul 17 at 18:58