ThenInclude 在 EF Core 查询中无法识别

编程入门 行业动态 更新时间:2024-10-12 20:24:01
本文介绍了ThenInclude 在 EF Core 查询中无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 IQueryable 看起来像这样:

IQueryable查询 = context.Set();query = query.Include("Car").ThenInclude("Model");

IQueryable"不包含ThenInclude"的定义并且没有扩展方法ThenInclude"接受第一个参数可以找到类型IQueryable"(您是否缺少使用指令或程序集引用?)

我有需要的所有参考资料:

使用 Content.Data.Models;使用 Microsoft.EntityFrameworkCore;使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Linq.Expressions;使用 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.我正在使用这个通用代表:

公共类 ReadOnlyRepository: IReadOnlyRepository其中 TContext : DbContext{受保护的只读 TContext 上下文;公共 ReadOnlyRepository(TContext 上下文){this.context = 上下文;}私有 IQueryableGetQueryable(表达式<Func<TEntity,bool>过滤器 = 空,FuncorderBy = 空,字符串 includeProperties = null,内部?跳过 = 空,内部?取=空)其中 TEntity :类,IEntity{包含属性 = 包含属性 ??字符串.空;IQueryable查询 = context.Set();如果(过滤器!= null){查询 = query.Where(filter);}foreach (var includeProperty in includeProperties.Split(新字符[] { ',' }, StringSplitOptions.RemoveEmptyEntries)){查询 = query.Include(includeProperty);}如果(orderBy != null){查询 = orderBy(查询);}如果(跳过.HasValue){查询 = query.Skip(skip.Value);}如果(采取.HasValue){查询 = query.Take(take.Value);}返回查询;}

解决方案

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

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");

更多推荐

ThenInclude 在 EF Core 查询中无法识别

本文发布于:2023-11-13 22:54:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法识别   ThenInclude   EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!