Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator Contains is translated in EF Core 1.x, but throws exception in 2.0. #10868

Closed
Dixin opened this issue Feb 3, 2018 · 1 comment
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Dixin
Copy link

Dixin commented Feb 3, 2018

Operator Contains is translated in EF Core 1.x, but throws exception in 2.0.

Recently I upgraded EF Core 1.x to 2.0 and got this problem. Take the AdventureWorks database's Product table as an example:

IQueryable<Product> source = adventureWorks.Products;
Product single = source.Single(product => product.ListPrice == 20.24M); // Execute query.
bool contains = source
    .Where(product => product.ProductSubcategoryID == 7)
    .Contains(single).WriteLine();

In 1.x the above query is translated to:

SELECT [product].[ProductID], [product].[ListPrice], [product].[Name], [product].[ProductSubcategoryID]
FROM [Production].[Product] AS [product]
WHERE [product].[ProductSubcategoryID] = 7

In 2.0 the above query throws ArgumentException:

Expression of type 'Product' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer' of method 'Boolean Contains[ValueBuffer](System.Collections.Generic.IEnumerable`1 [Microsoft.EntityFrameworkCore.Storage.ValueBuffer], Microsoft.EntityFrameworkCore.Storage.ValueBuffer)'
Parameter name: arg1
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0, Expression arg1)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 1 arguments)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression[] arguments)
at Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.CallWithPossibleCancellationToken(MethodInfo methodInfo, Expression[] arguments)
at Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.HandleContains(EntityQueryModelVisitor entityQueryModelVisitor, ContainsResultOperator containsResultOperator, QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.<>c.<.cctor>b__33_6(EntityQueryModelVisitor v, ResultOperatorBase r, QueryModel q)
at Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandlerContext.EvalOnClient(Boolean requiresClientResultOperator)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandleContains(HandlerContext handlerContext)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, Int32 index)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, Int32 index)
at Remotion.Linq.QueryModelVisitorBase.VisitResultOperators(ObservableCollection`1 resultOperators, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_0`1 .b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at System.Linq.Queryable.Contains[TSource](IQueryable`1 source, TSource item)
at Tutorial.LinqToEntities.QueryMethods.ContainsEntity(AdventureWorks adventureWorks) in D:\Dixin\GitHub\CodeSnippets\FunctionalProgramming+LINQ\Tutorial.Shared\LinqToEntities\QueryMethods.cs:line 1890
at Tutorial.Tests.LinqToEntities.QueryMethodsTests.QuantifiersTest() in D:\Dixin\GitHub\CodeSnippets\FunctionalProgramming+LINQ\Tutorial.Tests.Shared\LinqToEntities\QueryMethodsTests.cs:line 360

Further technical details

EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: (e.g. Visual Studio 2017 15.5.5)

@ajcvickers ajcvickers added this to the 2.1.0 milestone Feb 5, 2018
@smitpatel
Copy link
Contributor

Hijacking this!

@smitpatel smitpatel assigned smitpatel and unassigned maumar Feb 7, 2018
smitpatel added a commit that referenced this issue Feb 7, 2018
Contains on top-level cannot be translated (since we don't have it inside lambda and don't have expression tree for Item)
This caused to throw exception because we did not mark the QSRE for materialization.
Fix is to mark the naked QSRE for materialization if in top level Contains

Resolves #10868
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 7, 2018
smitpatel added a commit that referenced this issue Feb 7, 2018
Contains on top-level cannot be translated (since we don't have it inside lambda and don't have expression tree for Item)
This caused to throw exception because we did not mark the QSRE for materialization.
Fix is to mark the naked QSRE for materialization if in top level Contains

Resolves #10868
anpete added a commit that referenced this issue Feb 7, 2018
anpete added a commit that referenced this issue Feb 8, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants