I'm trying to make EntityFramework detect the needed properties and load them in a Run-time in my Repository class. As I'm working on EntityFramework 7, Is hack a lack of functions in comparison with EF6 so I've decided to use Reflection and try 2 possible ways to solve the problem. First way is to generate MemberExpression expression
and put it into query.Include(expression);
– It compiles fine but information Isn't loaded. Here is my source:
public virtual async Task<ActionResult> LoadAll()
{
using (var context = new CoreDataContext())
{
var data = context.Set<TEntity>();
foreach (var prop in typeof(TEntity).GetProperties())
{
//For testing purposes!
//I'm testing this only for my Customer class.
if (prop.Name != "Customer") continue;
var type = typeof(TEntity);
var param = Expression.Parameter(type, "x");
var memberAccess = Expression.PropertyOrField(param, prop.Name);
var expression = Expression.Lambda<Func<TEntity, Customer>>(memberAccess, param);
data.Include(expression);
}
await data.LoadAsync();
return Json(data, JsonConfig.GetSettings(typeof(TEntity)));
}
}
The second way is to generate context.Set<Entity>.Load()
somehow via reflection explicitly (where Entity should be classes of needed properties) but I have no idea how to execute the context.Set<Entity>.Load()
in a run-time. Could you tell me if there is another way to preload some data in Entity Framework 7 or how can I use reflection (If it even possible) to invoke context.Set<Entity>.Load()
?
Aucun commentaire:
Enregistrer un commentaire