No coercion operator is defined between types 'Microsoft.EntityFrameworkCore.Query.Internal.AnonymousObject' and 'YourEntityObject'.
With the latest release (1.7.16) of Z.EntityFramework.Plus.EFCore we started to get exceptions with several of our queries. They look something like the exception and stack trace below. They seem to be in only very specific scenarios, some of which we were able to work around. The first was just doing a simple row count with a .Include as part of the expression. The second is a much more basic query that is a basic pagination expression which we couldn't work around. However, rolling back to release 1.7.15 seems to resolve the issue.
var results = this.dbset.Include(...)
.Include(..)
.OrderByDescending(_ => _.ImportDate)
.Skip(offset)
.Take(count)
.ToList();
System.InvalidOperationException: 'No coercion operator is defined between types 'Microsoft.EntityFrameworkCore.Query.Internal.AnonymousObject' and 'YourEntity'.'
System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore<System.Collections.Generic.IEnumerable<YourEntity>>(System.Linq.Expressions.Expression query, Remotion.Linq.Parsing.Structure.INodeTypeProvider nodeTypeProvider, Microsoft.EntityFrameworkCore.Storage.IDatabase database, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Query> logger, System.Type contextType) Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute.AnonymousMethod__0() Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<System.Collections.Generic.IEnumerable<YourEntity>>(object cacheKey, System.Func<System.Func<Microsoft.EntityFrameworkCore.Query.QueryContext, System.Collections.Generic.IEnumerable<YourEntity>>> compiler) Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<System.Collections.Generic.IEnumerable<YourEntity>>(object cacheKey, System.Func<System.Func<Microsoft.EntityFrameworkCore.Query.QueryContext, System.Collections.Generic.IEnumerable<YourEntity>>> compiler) Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<System.Collections.Generic.IEnumerable<YourEntity>>(System.Linq.Expressions.Expression query) Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<System.Collections.Generic.IEnumerable<YourEntity>>(System.Linq.Expressions.Expression expression) Unknown
Remotion.Linq.dll!Remotion.Linq.QueryableBase<YourEntity>.GetEnumerator() Unknown
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.IncludableQueryable<YourEntity, YourEntityLead>.GetEnumerator() Unknown
System.Linq.dll!System.Collections.Generic.EnumerableHelpers.ToArray<YourEntity>(System.Collections.Generic.IEnumerable<YourEntity> source, out int length) Unknown
System.Linq.dll!System.Linq.Buffer<YourEntity>.Buffer(System.Collections.Generic.IEnumerable<YourEntity> source) Unknown
System.Linq.dll!System.Linq.OrderedEnumerable<YourEntity>.ToList(int minIdx, int maxIdx) Unknown
System.Linq.dll!System.Linq.OrderedPartition<System.__Canon>.ToList() Unknown
Hello @ischyrus ,
Do you think you could provide us a test project with this issue?
It will make easier/faster for my developer to getting started for investigating it.
We now always ask for a project since we found out that most issues are missing some essential information or are resolved by the requestor when creating it
(Even if the issue seem very easy to reproduce, by getting a test project, it allow us to give a faster support and better experience for the support of all our free libraries)
Best Regards,
Jonathan
Hi Jonathon,
I have also encountered this issue.
I upgraded from .NET Core 2.0 to .Net Core 2.1, including upgrading:
Microsoft.EntityFrameworkCore.SqlServer from 2.0.1 to 2.1.0 Microsoft.EntityFrameworkCore.Tools from 2.0.1 to 2.1.0 Z.EntityFramework.Plus.EFCore from 1.7.14 to 1.7.20
Like ischyrus, we have had to downgrade to 1.7.15 as well. 1.7.16 throws the error, so the issue was created between 1.7.15 and 1.7.16. It is caused by the use of Z.EntityFramework.Plus.QueryFilterExtensions.Filter extension method.
Removing the .Include() in the IQueryable stopped the exception throwing. I had a brain-melt when first encountering this issue and throught it was an issue in EF itself, so I double-checked our database structure vs our C# classes and everything looks fine.
Below is the stracktrace with our classes renamed:
System.InvalidOperationException: No coercion operator is defined between types 'Microsoft.EntityFrameworkCore.Query.Internal.AnonymousObject' and 'XX.YYY.Data.Models.Test'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
at Remotion.Linq.Clauses.ResultOperators.CastResultOperator.GetOutputDataInfo(IStreamedDataInfo inputInfo)
at Remotion.Linq.QueryModel.<>c.<GetOutputDataInfo>b__10_0(IStreamedDataInfo current, ResultOperatorBase resultOperator)
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
at Remotion.Linq.QueryModel.GetOutputDataInfo()
at Remotion.Linq.Clauses.Expressions.SubQueryExpression..ctor(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.CollectionQueryModelRewritingExpressionVisitor.CreateJoinToParentQuery(QueryModel parentQueryModel, QuerySourceReferenceExpression parentQuerySourceReferenceExpression, Expression outerTargetExpression, IForeignKey foreignKey, QueryModel targetQueryModel, ICollection`1 subQueryProjection)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.CollectionQueryModelRewritingExpressionVisitor.Rewrite(QueryModel collectionQueryModel, INavigation navigation)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.CollectionQueryModelRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor visitor, BlockExpression block)
at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.CollectionQueryModelRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.CollectionQueryModelRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Remotion.Linq.Clauses.SelectClause.TransformExpressions(Func`2 transformation)
at Remotion.Linq.QueryModel.TransformExpressions(Func`2 transformation)
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.RewriteCollectionQueries(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileAsyncQuery[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass22_0`1.<CompileAsyncQuery>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddAsyncQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQuery[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.System.Collections.Generic.IAsyncEnumerable<TResult>.GetEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.IncludableQueryable`2.System.Collections.Generic.IAsyncEnumerable<TEntity>.GetEnumerator()
at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken)
at XX.YYY.Api.Features.Test.GetTest.Handler.Handle(Command message, CancellationToken cancellationToken) in C:\\Git\\XX.YYY\\src\\XX.YYY.Api\\Features\\Test\\GetTest.cs:line 32
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at XX.YYY.Api.Controllers.TestController.Get() in C:\\Git\\XX.YYY\\src\\XX.YYY.Api\\Controllers\\TestController.cs:line 27
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
I hope this helps, Malcolm
Hello Malcolm,
Thank you for pointing out.
I believe the issue is causing because of the following changes:
- EF Core Query Filter now use ForceCast=true as default value
It was false before (We though everything was fixed in EF Core but apparently no)
You can always restore the old behavior with the following code:
QueryFilterManager.ForceCast = false;
I'm not sure if we should restore or not the ForceCast = false; as we will impact again people.
Best Regards,
Jonathan
Hi Jonathan,
I can confirm that using "QueryFilterManager.ForceCast = false;" fixes the issue I was having.
Thanks, Malcolm
Why I can see the Class QueryFilterManager ? Which Assembly?
I am use er core 2.1.4. and I got error info "The binary operator Equal is not defined for the types 'System.Boolean' and 'System.Int16'.".
Hello @laball ,
Are you getting this error in EF Plus? I'm not sure with your message if that's related or not to our library.