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

EF Core 3.0 Preview 8: Where condition including a boolean.ToString() throw exception #17197

Closed
gustavnavar opened this issue Aug 16, 2019 · 4 comments

Comments

@gustavnavar
Copy link

gustavnavar commented Aug 16, 2019

This query thows an exception when materialized:
var records = Context.Set().Where(x => x.Customer.IsVip.ToString().Contains(value));
where IsVip is a boolean field.

It happens when using EF Core 3.0 Preview 8. But it worked until EF Core 3.0 Preview 5.

Exception message: 
Operation is not valid due to the current state of the object.
[EFCoreTest.zip](https://github.com/aspnet/EntityFrameworkCore/files/3510065/EFCoreTest.zip)


Stack trace:
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   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__DisplayClass9_0`1.<Execute>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.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at EFCoreTest.Data.OrdersRepository.Count(String value) in C:\source\repos\EFCoreTest\EFCoreTest\Data\OrdersRepository.cs:line 39

Steps to reproduce

Run the attached project. It includes the Northwind sample database in the Database folder.

var records = Context.Set<Order>().Where(x => x.Customer.IsVip.ToString().Contains(value));
return records.Count();

Further technical details

EF Core version: EF Core 3.0 Preview 8
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Pro 1903
IDE: Visual Studio 2019 v. 16.3.0 Preview 2.0

EFCoreTest.zip

@smitpatel
Copy link
Member

Duplicate of #14205

@smitpatel smitpatel marked this as a duplicate of #14205 Aug 16, 2019
@gustavnavar
Copy link
Author

Hi,

the issue that you refer is not the same. The other one is about how boolean functions are converted to SQL depending on the database provider.

This one is about a code throwing an exception.

As I explain before, this code was working as expected ith EF Core 2.2 and EF Core 3.0 Preview 5. And now it throws an exception when using EF Core 3.0 Preview 8.

I've attached an example of the code throwing the exception.

@smitpatel
Copy link
Member

The issue linked is talking about translating ToString function on Boolean column. Earlier we translated it to server but it was incorrect translation. See #14205 (comment)

Hence we decided to block the translation in short term. In future we would add translation to server which is being tracked by #14205
Incorrect results can cause potential data corruption in database hence, those are higher priority to fix than exception.

@gustavnavar
Copy link
Author

Understood. Thanks for the explanation !!

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants