Entity Framework Include() returns null navigational property

asp.net-mvc c# entity-framework entity-framework-6

Question

I am having issue 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 like 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 the list of Teams with a NULL Owner property. Any idea what I am missing here?

1
4
11/22/2015 9:49:55 AM

Accepted Answer

You must use from 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 multiple includes

GetManyAsync(filter ,p => p.prop1 ,p.prop2,...)
2
11/23/2015 5:54:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow