Entity Framework Include () returns null navigational property

I have a problem with the Include function. I have a Team class that has an Owner property of type Owner. I have a helper function that wraps my EF calls, as shown below:

public Task<List<T>> GetManyAsync(
    Expression<Func<T, bool>> filter = null,
    Expression<Func<T, object>> includeProperties = null)
{
    IQueryable<T> query = _dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    if (InstanceHelper.IsSomething(includeProperties))
    {
        query.Include(includeProperties);
    }

    return query.ToListAsync();
}

And I use it like this

var teams = await DataAccess.Team.GetManyAsync(e => e.Owner.Id == userId, e => e.Owner);

But it returns a list of commands with the NULL Owner property. Any idea what I'm missing here?

+4
source share
1 answer

You have to use this

public Task<List<T>> GetManyAsync(Expression<Func<T, bool>> filter = null, params Expression<Func<T, object>>[] includeProperties = null)
{
  foreach (var prop in includeProperties)
  query = query.Include(prop);
  ...
}

And you can have several inclusions

GetManyAsync(filter ,p => p.prop1 ,p.prop2,...)
+2
source

Source: https://habr.com/ru/post/1616924/


All Articles