![](/img/trans.png)
[英]EF Core “InvalidOperationException: Include has been used on non entity queryable” on IQueryable
[英]EF Core "InvalidOperationException: Include has been used on non entity queryable" for navigation property even after explicit include
我創建了一個使用以下包的示例項目 -
GraphQL
GraphQL.EntityFramework
System.Linq.Dynamic.Core
這里的想法是轉換在 GraphQL 查詢中完成的選擇並將其傳遞到 EntityFramework 上,以免過度獲取查詢中未要求的列。 為此,我使用System.Linq.Dynamic.Core
在字符串上傳遞表單中的Select
表達式。 我已經看到 EntityFramework github repo 上存在一個問題,用於獲取導航屬性,其中指出 include 是必須的,如這里所討論的。 之后,我在進行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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.