efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Sqlite Support translation of ToString

Open etcircle opened this issue 7 years ago • 8 comments

Describe what is not working as expected. First, I am new to asp core and this may not be expected to work like that.

I am trying to convert an Id (int) to a string so I can use Contains method to search using a pattern. However, the code breaks when I have ToString used on int.

Query.Where(s => s.Id.ToString().Contains("something"));

Example of my code materialQuery = materialQuery.Where(s => s.Name.ToLower().Contains(searchString.ToLower()) || s.Id.ToString().Contains(searchString));

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception has occurred: CLR/System.InvalidOperationException An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Operation is not valid due to the current state of the object.' 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.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_01.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 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.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable1 source, Expression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) at BAServer.Models.Helpers.PagedList1.<CreateAsync>d__17.MoveNext() in /Users/codingaccountant/CodingAccountant/BAServer/Models/Helpers/PagedList.cs:line 29 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at BAServer.Pages.IM.Master_Data.IndexMaterial.d__11.MoveNext() in /Users/codingaccountant/CodingAccountant/BAServer/Pages/IM/Master Data/IndexMaterial.razor:line 111 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at BAServer.Pages.IM.Master_Data.IndexMaterial.d__9.MoveNext() in /Users/codingaccountant/CodingAccountant/BAServer/Pages/IM/Master Data/IndexMaterial.razor:line 87


### Further technical details
EF Core version: 3.0 Preview 8
Database Provider: SQLite
Operating system: macOS
IDE: VS Code

etcircle avatar Aug 17 '19 14:08 etcircle

Is the following, the reason why it would not work as I expected?

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client

etcircle avatar Aug 17 '19 15:08 etcircle

Sqlite provider does not have support for ToString methods

smitpatel avatar Aug 19 '19 18:08 smitpatel

Ah, thanks for the answer!

etcircle avatar Aug 20 '19 09:08 etcircle

Assigning to @bricelam since PR is assigned to him.

smitpatel avatar Feb 14 '20 20:02 smitpatel

We use SQLite for our unit tests (and SQL Server for production) and it works great for the most part but I just hit an issue with converting numbers to strings and the workaround I found for that is to explicitly call ToString(), which then breaks all the relevant tests. The conversion issue is https://github.com/dotnet/efcore/issues/20732

jons-aura avatar Apr 23 '20 16:04 jons-aura

Passing to @maumar

smitpatel avatar Jun 10 '20 19:06 smitpatel

If there's a point release before 6.0.0 will this make it into that or will this definitely not land before 6.0.0? I did some ugly work arounds for this once and just hit another place it looks like I'll have to do that again and would really rather not.

jons-aura avatar Feb 01 '21 16:02 jons-aura

@jons-aura as this is a feature, we won't be backporting this to 5.0.x; we only patch for bugs. However, this will be out in a couple weeks as 6.0.0-preview1, and our previews are quite stable (undergo the same test verification suite etc.), and we react quickly to regressions found on them - so I'd consider using that.

roji avatar Feb 01 '21 16:02 roji