然后包含在EF Core查询中无法识别 [英] ThenInclude not recognized in EF Core query

查看:237
本文介绍了然后包含在EF Core查询中无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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?

查询:

[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.我正在使用thise通用代表:

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仅在将Include重载与lambda表达式参数一起使用时可用:

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

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

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

My IQueryable looks like this:

 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?)

I have all references needed:

using Content.Data.Models;    
using Microsoft.EntityFrameworkCore;    
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Linq.Expressions;    
using System.Threading.Tasks;   

Why it doesn't recognize ThenInclude?

Query:

[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")}

Fails after I include .Include("BaseContentItem.TopicTag") part.

So I just read that with generic repository you lose ThenInclude. I am using thise generic rep:

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 is available only when you use the Include overload with lambda expression parameter:

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

When you use the Include overload with string argument, there is no need of ThenInclude since you can specify the whole property path in the passed string:

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

这篇关于然后包含在EF Core查询中无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆