So I’m attempting to dynamically load my domain data service where the table name is the string … Here’s what I’ve got so far: Normally I’d load like this:
theDomainDataService.Load(theDomainDataService.getUsersQuery());
so I’m trying to automate which entity is loaded by the string name.
String theVariableEntityName = "Users";
Type t = theDomainDataService.GetType();
MethodInfo stuff = t.GetMethod("Get" + theVariableEntityName + "Query");
var theQuery = stuff.Invoke(theDomainDataService, null);
theDomainDataService.Load((EntityQuery<MySite.Web.Models.User>)theQuery);
---------------------------------------------------------^ Problem
This is in fact loading my domainDataService correctly, but what I need is a dynamic way to infer the type of the EntityQuery (without explicitly declaring it’s going to be a User), because in fact it could be anything.
I have tried this from the DomainDataService Class with no luck, it isn’t finding method’s “Set” or “Entry”.
public List<object> void PopulateEntity(string theEntityName)
{
Type theEntity = Type.GetType("MySiteMaintenance.Web.Models." + theEntityName);
using (var db = new DatingEntities())
{
IQueryable query = db.Set(theEntity);
foreach (var item in query)
{
var entry = db.Entry(item);
}
}
}
Remember, all I need is a populated entity (when all I have is the name of the entity) populated Client side… so I can say
DomainServiceClass theClass = new DomainServiceClass();
theClass.Load(theClass.GetEntityNameQuery());
so I can reference the appropriately loaded entity with…
theClass.Entity (users… questions, etc..)
I’m still not sure I follow, but…
I have a
Postentity in mySandboxnamespace which I get from myDbContextinstance using the entity type name in a string to start with…Which results in retrieving any DbSet based on a Entity type string. Below a breakpoint in the item foreach loop – revealing the values.