ThenInclude 在 EF Core 查询中无法识别
Posted
技术标签:
【中文标题】ThenInclude 在 EF Core 查询中无法识别【英文标题】:ThenInclude not recognized in EF Core query 【发布时间】:2017-09-01 12:47:57 【问题描述】:我的 IQueryable 看起来像这样:
IQueryable<TEntity> query = context.Set<TEntity>();
query = query.Include("Car").ThenInclude("Model");
“IQueryable”不包含“ThenInclude”的定义 并且没有扩展方法“ThenInclude”接受第一个参数 可以找到类型“IQueryable”(您是否缺少使用 指令还是程序集引用?)
我有所有需要的参考资料:
using Content.Data.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
为什么它不识别 ThenInclude?p>
查询:
[Content.Data.Models.Article]).Where(x => (((x.BaseContentItem.SiteId == value(Content.Business.Managers.ArticleManager+<>c__DisplayClass8_0).id) AndAlso x.BaseContentItem.IsActive) AndAlso x.BaseContentItem.IsLive)).Include("BaseContentItem").Include("BaseContentItem.TopicTag").Include("MainImage")
在我包含 .Include("BaseContentItem.TopicTag")
部分后失败。
所以我刚刚读到,使用通用存储库你会丢失 ThenInclude。我正在使用这个通用代表:
public class ReadOnlyRepository<TContext> : IReadOnlyRepository
where TContext : DbContext
protected readonly TContext context;
public ReadOnlyRepository(TContext context)
this.context = context;
private IQueryable<TEntity> GetQueryable<TEntity>(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = null,
int? skip = null,
int? take = null)
where TEntity : class, IEntity
includeProperties = includeProperties ?? string.Empty;
IQueryable<TEntity> query = context.Set<TEntity>();
if (filter != null)
query = query.Where(filter);
foreach (var includeProperty in includeProperties.Split
(new char[] ',' , StringSplitOptions.RemoveEmptyEntries))
query = query.Include(includeProperty);
if (orderBy != null)
query = orderBy(query);
if (skip.HasValue)
query = query.Skip(skip.Value);
if (take.HasValue)
query = query.Take(take.Value);
return query;
【问题讨论】:
EF Core 是否还有.Include(string)
和 .ThenInclude(string)
方法?不应该是query = query.Include(e => e.Car).ThenInclude(e => e.Model);
吗?
EntityFramework Core 1.1.0 missing Include()?的可能重复
@DavidG 确实。或者只是Include(dot_separated_string)
。但是 OP 声称它不起作用,而且我不记得从引入重载(如果我没记错的话是 1.1.0)时点分隔的属性名称 Include
有问题。
@IvanStoev 啊,是的,这是可能的。从来都不是基于字符串的版本的粉丝,因为它们缺少编译时类型检查。
编辑后不清楚你在问什么。
【参考方案1】:
ThenInclude
仅在您使用带有 lambda 表达式参数的 Include
重载时可用:
query = query.Include(e => e.Car).ThenInclude(e => e.Model);
当您使用带有字符串参数的Include
重载时,不需要ThenInclude
,因为您可以在传递的字符串中指定整个属性路径:
query = query.Include("Car.Model");
【讨论】:
它对我有用。考虑发布一个重现(并指定 EF Core 版本 - 我在 v2.0 上的测试) 我在这里打开了新问题***.com/questions/46003275/… 我看到了。你最后写的让我认为堆栈溢出异常可能不是由Include
方法引起的(正如我所说的在我的测试中有效),而是在BaseContentItem.TopicTag
生效时由AutoMapper 引起的(可能是@987654330 @ 具有对 BaseContentItem
的循环引用,这导致映射代码中的无限循环)。您能否验证查询是否可以在不调用 Map
的情况下执行并具体化到 List 中?【参考方案2】:
您的问题可能是缺少对 Microsoft.EntityFrameworkCore.Relational 的引用?
这可以通过包管理器添加。
还要确保您使用的是 Microsoft.EntityFrameworkCore 命名空间中的 .include,而不是其他 .Include(没有 .ThenInclude)
【讨论】:
请edit 将其改写为有条件的答案,例如“您的问题可能是……,在这种情况下,解决方案是……”。目前的措辞方式可能会被误认为是一个澄清问题(应该在评论中......)。以上是关于ThenInclude 在 EF Core 查询中无法识别的主要内容,如果未能解决你的问题,请参考以下文章
如何在 EF Core 中调用 ThenInclude 两次?
如何在 .net core ef 上应用 thenInclude 条件?
Entity Framework Core 如何在不使用 Include().ThenInclude() 的情况下从模型中列出一对多从多对多