简体   繁体   English

实体框架Include()返回null导航属性

[英]Entity Framework Include() returns null navigational property

I am having issue with the Include function. 我遇到包含功能的问题。 I have a Team class that has an Owner property of type Owner. 我有一个Team类,其Owner属性为Owner。 I have a helper function that wraps my EF calls like below; 我有一个辅助函数,它包含我的EF调用,如下所示;

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. 但它返回具有NULL Owner属性的团队列表。 Any idea what I am missing here? 知道我在这里缺少什么吗?

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,...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架6和导航属性问题 - Entity Framework 6 and navigational property issue 在 EF Core 中保存实体时,仅使用导航属性的 ID 会为该属性返回 null - When saving an entity in EF Core, using only an ID for a navigational property returns null for that property 实体框架为包含属性返回null - Entity Framework returns null for Include properties 实体框架3 - 导航属性为空且包含不同的包含() - Entity Framework 3 - Navigation Property Null with Varying Include() 筛选器导航属性实体框架,返回NULL - Filter Navigation Property Entity Framework, returns NULL 实体框架核心包括加载额外的导航属性 - Entity Framework Core include loading extra navigational properties 实体框架-仅加载导航属性中的某些行 - Entity Framework - Loading only certain rows in navigational property 实体框架 - 为集合中的成员加载特定的导航属性 - Entity Framework - Loading specific Navigational Property for members in collection 我如何模拟实体框架的导航财产情报? - How Do I Mock Entity Framework's Navigational Property Intelligence? 首先 Enitify Framework 代码:使用所需的相关导航属性更新实体 - Enitify Framework code first: Updating Entity With required related navigational property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM