c# - ThenInclude 在 EF Core 查询中无法识别

标签 c# entity-framework linq entity-framework-core

我的 IQueryable 看起来像这样:

 IQueryable<TEntity> query = context.Set<TEntity>();
query = query.Include("Car").ThenInclude("Model");

'IQueryable' does not contain a definition for 'ThenInclude' and no extension method 'ThenInclude' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)

我有所有需要的引用资料:

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?

查询:

[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;
        }

最佳答案

ThenInclude 仅在您使用带有 lambda 表达式参数的 Include 重载时可用:

query = query.Include(e => e.Car).ThenInclude(e => e.Model);

当您使用带字符串参数的 Include 重载时,不需要 ThenInclude 因为您可以在传递的字符串中指定整个属性路径:

query = query.Include("Car.Model");

关于c# - ThenInclude 在 EF Core 查询中无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46000602/

相关文章:

C#解决从不同数字组中选择2个数字的多种方法

c# - 训练模型而不在 ML.NET 中标记特征

如果 C# Windows 服务包含(嵌入) Entity Framework ,则不会安装

c# - 如何在条件下使用 RemoveAll 删除列表中的多个项目?

c# - 使用 Rx 尝试某件事 5 次

c# - 调用 Restful Wcf 服务时出现 404 错误

c# - 工作单元/存储库模式和使用 EF 更新实体

c# - 使用 C#/Moq 框架模拟带有输入参数的异步 DbContext 函数

c# - 使用linq将对象集合的列表成员的内容组合成单个列表

c# - 打开“获取目录”对话框时出现 COM 类工厂错误 80040154