I have some (non)generic functions explicitly assigned to a DbSet
(using Entity Framework 6.1, but the question is more generic, in a sense):
//Non-generic type method
public static IQueryable BuildQuery(this DbSet dbSet)
{
return dbSet;
}
//Generic base class elements method
public static IQueryable<Entity> BuildQuery(this DbSet<Entity> dbSet)
{
return dbSet.Include(de1 => de1.NavigationPropertyBase);
}
//Derived class 1 elements method
public static IQueryable<DerivedEntity1> BuildQuery(this DbSet<DerivedEntity1> dbSet)
{
return dbSet.Include(de1 => de1.NavigationPropertyX);
}
//Derived class 2 elements method
public static IQueryable<DerivedEntity2> BuildQuery(this DbSet<DerivedEntity2> dbSet)
{
return dbSet.Include(de2 => de2.NavigationPropertyX).
Include(de2 => de2.NavigationPropertyY);
}
I know that although DerivedEntity1
and DerivedEntity2
extend Entity
, Dbset<DerivedEntityX>
does not extend DbSet<Entity>
.
What I want to do though, is achieve a late-binding like behavior, based on the generic type. I thought to put the generic-type into a non-generic variable, and then invoke the BuildQuery
method (visible as it exists for the non-generic type at compile time):
//This compiles with no errors.
DbSet dbSetNonGeneric = dbSet; // dbSet is of DbSet<DerivedEntity1> type
var result = dbSetNonGeneric.BuildQuery();
My question is, will this lead to an invocation of the BuildQuery
method according to the generic type or will it invoke the non-generic method? And in the second case, is there a way to achieve that kind of method invocation?