asp.net-core - 将 Lambda 作为参数传递到 EFCore 中的存储库方法

标签 asp.net-core entity-framework-core

我正在尝试实现描述的存储库模式 here

但是,这些关系并未包含在内。

public IEnumerable<MyEntity> GetWithRelationship(params Expression<Func<MyEntity, object>>[] includeProperties)
{
  var set = _context.MyEntities;
  foreach (var includeProperty in includeProperties)
  {
    set.Include(includeProperty);
  }
  return set.ToList();
}

但是以下方法有效:

return _context.MyEntities.Include(x => x.RelatedEntity).ToList();

最佳答案

您必须重新分配setInclude() 的返回值方法。但是,为了避免编译错误,您必须处理 setIQueryable<MyEntity>第一:

var set = _context.MyEntities as IQueryable<MyEntity>;

foreach (var includeProperty in includeProperties)
{
    set = set.Include(includeProperty);
}

return set.ToList();

关于asp.net-core - 将 Lambda 作为参数传递到 EFCore 中的存储库方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250040/

相关文章:

asp.net-core - 使用 ASP.NET 5 导出为 pdf

c# - ViewBag 没有向 View 发送任何值

elasticsearch - Elasticsearch错误 “top is below bottom corner error”

c# - 实体 ForEach [JsonIgnore]

c# - 无法安装 dotnet-aspnet-codegenerator 工具

c# - 增加使用 Azure 部署的 K8s 集群中的最大文件上传大小(使用 C# 和 React 的 ASP.NET Core 中的项目)

c# - Asp.net core中如何在ViewComponent之间共享ViewData

c# - Entity Framework Core 使用 Windows 服务

c# - 隔离 EF Core 中的更改

c# - .net 核心 Entity Framework (EF Core) 表命名约定