gpt4 book ai didi

c# - 即使在显式包含之后,导航属性的 EF Core "InvalidOperationException: Include has been used on non entity queryable"

转载 作者:行者123 更新时间:2023-12-05 07:15:14 24 4
gpt4 key购买 nike

我创建了一个使用以下包的示例项目 -

  1. GraphQL
  2. GraphQL.EntityFramework
  3. System.Linq.Dynamic.Core

这里的想法是转换在 GraphQL 查询中完成的选择并将其传递到 EntityFramework,以免过度获取查询中不需要的列。为此,我使用 System.Linq.Dynamic.Core 以字符串形式传递 Select 表达式。我已经看到 EntityFramework github 存储库中存在用于获取导航属性的问题,其中指出必须包含,如所讨论的 here .之后,我已经包含了在进行 Select 之前所需的导航属性,但由于某种原因它会提示以下错误 -

GraphQL.ExecutionError: Error trying to resolve customers.
---> System.InvalidOperationException: Include has been used on non entity queryable.
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression source, Expression expression, Boolean thenInclude)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at GraphQL.EntityFramework.EfGraphQLService`1.<>c__DisplayClass26_0`2.<<BuildQueryField>b__0>d.MoveNext() in C:\\projects\\graphql-entityframework\\src\\GraphQL.EntityFramework\\GraphApi\\EfGraphQLService_Queryable.cs:line 130
at GraphQL.Instrumentation.MiddlewareResolver.Resolve(ResolveFieldContext context)
at GraphQL.Execution.ExecutionStrategy.ExecuteNodeAsync(ExecutionContext context, ExecutionNode node)

这是失败的代码段

public class QueryTest : QueryGraphType<TestDBContext>
{
public QueryTest(IEfGraphQLService<TestDBContext> graphQlService) :
base(graphQlService)
{
Name = "Query";
AddQueryField(
name: "customers",
// the next line is failing
resolve: context => context.DbContext.Customers.Include(x => x.Orders).Select<Customer>(GetSelect(context.SubFields))
);
AddQueryField(
name: "orders",
resolve: context => context.DbContext.Orders.Include(x => x.Customer).Select<Order>(GetSelect(context.SubFields))
);
}

private string GetSelect(IDictionary<string, Field> subfields) => $"new({string.Join(",", GetSelectedColumns(subfields))})";

private IEnumerable<string> GetSelectedColumns(IDictionary<string, Field> subfields)
{
foreach (var item in subfields)
{
if (item.Value.SelectionSet.Children.Count() > 0)
{
continue;
}
yield return item.Key;
}
}
}

当我发出以下 GraphQL 查询时

query {
customers {
customerName
orders {
orderID
orderDate
}
}
}

完整的代码示例托管在 github 上而不是将其粘贴到此处,因为阅读起来太长了。任何帮助将不胜感激。

最佳答案

你试过这个项目吗:https://github.com/StefH/GraphQL.EntityFrameworkCore.DynamicLinq

通过这个项目,您可以轻松地将 EF 实体的所有属性公开为 GraphQL 查询的可搜索字段。

例如,参见 https://github.com/StefH/GraphQL.EntityFrameworkCore.DynamicLinq/tree/master/examples/MyHotel

关于c# - 即使在显式包含之后,导航属性的 EF Core "InvalidOperationException: Include has been used on non entity queryable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59634540/

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