OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
856 stars 473 forks source link

Error when SkipToken contains a non-nullable string #2043

Open Grauenwolf opened 4 years ago

Grauenwolf commented 4 years ago

When sorting by two columns, the first being a string, it throws an exception when a $skiptoken is provided.

https://localhost:44322/odata/Transaction?$orderby=ContactCity&$skiptoken=ContactCity-'Atlanta',TxnId-13258

Assemblies affected

    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.3.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />

Reproduce steps

The simplest set of steps to reproduce the issue. If possible, reference a commit that demonstrates the issue.

Expected result

What would happen if there wasn't a bug.

Actual result

InvalidOperationException: The LINQ expression 'DbSet<Transaction>
.OrderBy(t => t.ContactCity)
.Where(t => string.Compare(
strA: t.ContactCity, 
strB: __TypedProperty_0, 
comparisonType: Ordinal) > 0 || t.ContactCity == __TypedProperty_0 && t.TxnId > __TypedProperty_1)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, ref <>c__DisplayClass8_0 )
TargetInvocationException: Exception has been thrown by the target of an invocation.
System.RuntimeMethodHandle.InvokeMethod(object target, object[] arguments, Signature sig, bool constructor, bool wrapExceptions)

Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, ref <>c__DisplayClass8_0 )
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor<TResult>(Expression query)
Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery<TResult>(Expression query, bool async)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore<TResult>(IDatabase database, Expression query, IModel model, bool async)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass9_0<TResult>.<Execute>b__0()
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<TFunc>(object cacheKey, Func<Func<QueryContext, TFunc>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<TResult>(object cacheKey, Func<Func<QueryContext, TResult>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<TResult>.GetEnumerator()
System.Collections.Generic.List<T>..ctor(IEnumerable<T> collection)
Microsoft.AspNet.OData.Query.TruncatedCollection<T>..ctor(IQueryable<T> source, int pageSize, bool parameterize)
Microsoft.AspNet.OData.Query.ODataQueryOptions.LimitResults<T>(IQueryable<T> queryable, int limit, bool parameterize, out bool resultsLimited)

Additional detail

I suspect that this is a breaking change caused by EF Core 3.x no longer allowing client-side evaluation. (Though to be fair, this query should be executed server-side.)

SiberaIndustries commented 4 years ago

I ran into a similar issue and as you already said, it is because of the disallowed client-side evaluation. In your case the client-side evaluation is probably triggered by the string.Compare clause (https://github.com/dotnet/efcore/issues/18020 describes exactly the same).

DbSet<Transaction>
.OrderBy(t => t.ContactCity)
.Where(

  /* This line may triggers a client-side evaluation */
  t => string.Compare(strA: t.ContactCity, strB: __TypedProperty_0, comparisonType: Ordinal) > 0 || 

  t.ContactCity == __TypedProperty_0 && 
  t.TxnId > __TypedProperty_1
)

Before EF Core 3.0, such queries were executed client-side anyway, so this is a good reason to work on it and make them more efficient :)