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

Generic repository with eager load using ThenInclude() method?

这是我的通用存储库:

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() 来解决这个问题,但我不知道如何在通用存储库中应用它。

非常感谢任何帮助。

谢谢

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