gpt4 book ai didi

c# - 使用 ThenInclude() 方法进行预加载的通用存储库?

转载 作者:行者123 更新时间:2023-11-30 18:15:02 25 4
gpt4 key购买 nike

这是我的通用存储库:

public class Repository<T> : IRepository<T> where T : BaseEntity
{
private DbContext _dbContext { get; set; }
private DbSet<T> _dbSet { get; set; }

public Repository(DbContext context)
{
this._dbContext = context;
this._dbSet = context.Set<T>();
}

public IQueryable<T> GetAll(params Expression<Func<T, object>>[] includes)
{
IQueryable<T> currentSet = this._dbSet;
foreach (var item in includes)
{
currentSet = currentSet.Include(item);
}
return currentSet;
}

public T Get(Expression<Func<T, bool>> predicated,
params Expression<Func<T, object>>[] includes)
=> this.GetAll(includes).Where(predicated).FirstOrDefault();
}

问题是当我使用预先加载来加载问题(包括它的答案)时,我无法查询答案的投票。

我很放心我收到了这个错误,因为我正在加载一个问题,包括它的答案并再次包括答案的投票。所以我尝试使用 ThenInclude() 来解决这个问题,但我不知道如何在通用存储库中应用它。

非常感谢任何帮助。

谢谢

最佳答案

您可以使用嵌套包含来解决您的问题。引用链接:EF LINQ include multiple and nested entities

关于c# - 使用 ThenInclude() 方法进行预加载的通用存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209129/

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