I'm working on creating a generic repository for an EF Core project to avoid having to write CRUD for all models. A major roadblock I've hit is navigation properties not being loaded since Core doesn't yet support lazy loading and the generic class obviously can't define .Include statements for class specific properties.
I'm trying to do something like this for my Get method to include all the properties dynamically:
public virtual T Get(Guid itemId, bool eager = false)
{
IQueryable<T> querySet = _context.Set<T>();
if (eager)
{
foreach (PropertyInfo p in typeof(T).GetProperties())
{
querySet = querySet.Include(p.Name);
}
}
return querySet.SingleOrDefault(i => i.EntityId == itemId);
}
But it throws an error when including properties that are not navigation properties.
I found this answer which is about the same thing but its for EF 5 and involves methods that are not present in EF core:
EF5 How to get list of navigation properties for a domain object
Is it possible to accomplish the same thing in EF Core?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…