相关疑难解决方法(0)

在 C# 通用存储库 EF 中包括孩子的孩子

我面临一个小问题,希望你能帮忙,基本上我想在通用存储库模式中得到孩子的孩子,因为我有多重关系的关系表

我的 Repository 方法如下所示:

public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includes)
    {
      var query = _entities.Where(predicate).AsQueryable();
      if (includes != null)
      {
        query = includes.Aggregate(query, (current, include) => current.Include(include));
      }
      return query;
    }

public async Task<IQueryable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includes)
    {
      return await Task.Run(() => Find(predicate, includes));
    }
Run Code Online (Sandbox Code Playgroud)

我的型号:

产品:

public class Product : BaseEntity<long>
  {
    [MaxLength(100)]
    public string Name { get; set; }
    [MaxLength(100)]
    public string Barcode { get; set; }
    public int ShelfLife { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core .net-core

5
推荐指数
0
解决办法
1076
查看次数

如何在Entity Framework Core中传递具有多个级别的lambda'include'?

我有一个存储库,可以获得'include'的lambda表达式.

public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includePaths)
    {
        return Context.Set<TEntity>().Includes(includePaths).FirstOrDefault(predicate);
    }
Run Code Online (Sandbox Code Playgroud)

在EF的早期版本中,我在服务层中使用它,例如:

var plan = _unitOfWork.PlanRepository
            .FirstOrDefault(
                p => p.Id == id, 
                include => include.PlanSolutions.Select(ps => ps.Solution)
            );
Run Code Online (Sandbox Code Playgroud)

其中'PlanSolutions'是一个集合,'Solution'是一个嵌套在'PlanSolution'中的属性.

但是现在这段代码出错了:

InvalidOperationException:属性表达式'include => {来自[include]中的PlanSolutions ps..PlanSolutions select [ps] .Solution}'无效.表达式应表示属性访问:'t => t.MyProperty'.有关包含相关数据的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=746393.

现在看来我不能使用'Select'方法来获取多个级别包含,但我也不能使用Microsoft建议的'ThenInclude'方法,因为查询本身位于存储库内部,服务没有访问.有没有办法治愈它?

c# asp.net lambda include entity-framework-core

2
推荐指数
3
解决办法
1921
查看次数