I am using the VS Lightswitch ServerApplicationContext to access and modify lightswitch data entities within an ApiController.
Let's say I have a Customer entity, and i can query the collection of customers in the lightswitch db using linq:
IEnumerable<Customer> customers = from custs in serverContext.DataWorkspace
.ApplicationData
.Customers
.GetQuery()
.Execute()
where c.AProperty == aProperty
select custs;
or
IEnumerable<Customer> customers =
serverContext.DataWorkspace
.ApplicationData
.Customers
.Where(c => c.AProperty == aProperty)
.Execute();
This works perfectly.
However, I have many more entities and several projects with different entities in each project and I am trying to create a library to allow me to query ServerApplicationContext
using reflection.
I have used reflection to get the properties of the ServerApplicationContext
object, which gives me access to the EntitySet<T>
, but I can not execute any queries against it.
This is the code as it stands:
Type t = serverContext.DataWorkspace.ApplicationData.GetType();
PropertyInfo[] pInfo = t.GetProperties();
foreach (var p in pInfo)
{
// p is equal to {Microsoft.LightSwitch.Framework.EntitySet`1[LightSwitchApplication.Customer] Customers}
MethodInfo mInfo = p.PropertyType.GetMethod("GetQuery");
var result = mInfo.Invoke(p.PropertyType, null) ; //<-- Error Here
}
The error returned is:
An exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll but was not handled in user code Additional information: Object does not match target type.
Has anyone had any joy with querying EntitySets
(including where clauses) using reflection?