gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:53:55 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com