gpt4 book ai didi

c# - Entity Framework Include() 返回 null 导航属性

转载 作者:行者123 更新时间:2023-11-30 12:24:33 25 4
gpt4 key购买 nike

我对 Include 函数有疑问。我有一个 Team 类,它有一个 Owner 类型的 Owner 属性。我有一个辅助函数来包装我的 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();
}

我是这样用的

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

但它会返回所有者属性为 NULL 的团队列表。知道我在这里缺少什么吗?

最佳答案

你必须从这里使用

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);
...
}

你可以有多个包含

GetManyAsync(filter ,p => p.prop1 ,p.prop2,...)

关于c# - Entity Framework Include() 返回 null 导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com