Previously (when using .net 4.5.2 and EF 6). I have had a generic Get
method that accepted a number of includes as follows;
public abstract class DataContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>, IDataContext
{
public DataContext(DbContextOptions options)
: base(options)
{
}
// reduced for brevity
public T Get<T>(int id, params Expression<Func<T, object>>[] includes) where T : class, IEntity
{
return this.Set<T>().Include(includes).FirstOrDefault(x => x.Id == id);
}
I would then call for example;
context.Get<Job>(id,
x => x.Equipment,
x => x.Equipment.Select(y => y.Type));
To include the Job.Equipment
and also the Job.Equipment.Type
.
However, when I have ported this over to asp.net core 2. I have tried the same generic approach, but if I try to include a sub-entity I get the following error;
The property expression 'x => {from Equipment y in x.Equipment select [y].Type}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
Can anyone suggest how I can work around this to include sub entities in my Generic Get<T>
method with Entity Framework Core 2?
Update
From looking at the documents there is an additional include method
include(string navigationPropertyPath)
I added the following method;
public T Get<T>(int id, string[] includes) where T : class, IEntity
{
var result = this.Set<T>().AsQueryable();
foreach(var include in includes)
{
result = result.Include(include);
}
return result.FirstOrDefault(x => x.Id == id);
}
Which does work, although I am not convinced on the efficiency here?