When querying entity framework for all objects of a given type, you'd do something like this:
List<MyDesiredObjects> = _myContext.MyDesiredObjects.ToList();
But is there a way one can create a function to query EF to find all objects of a given type if the type is variable? I've created this function, which at least compiles:
private List<TEntity> GetFromEF<TEntity>() where TEntity : class
{
MyDBEntities context = new UnityDBEntities(_entityConnection);
IObjectContextAdapter adapter = (IObjectContextAdapter)context;
System.Data.Objects.ObjectContext oContext = adapter.ObjectContext;
return oContext.CreateObjectSet<TEntity>().ToList();
}
But I'm struggling to see how I can call it, never mind actually extract data from it. Using reflection, like this (ucm.MappingType is a Type):
MethodInfo method = typeof(BaseXmlReader).GetMethod("GetFromEF");
MethodInfo gMethod = method.MakeGenericMethod( ucm.MappingType.GetType() );
var meh = gMethod.Invoke(null, null);
Fails with object reference not set to instance of object. And I've not even got to paging my list yet.
Any other way to approach this problem?
var list = GetFromEF<MyDesiredObjects>();No need to get into reflection. msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx