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

An exception occurred while reading a database value. The expected type was 'System.Decimal' but the actual value was of type 'System.Double' #13587

Closed
samzhou2 opened this issue Oct 11, 2018 · 8 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@samzhou2
Copy link

An aggregation query with cast will throw InvalidOperationException

Exception message:
An exception occurred while reading a database value. The expected type was 'System.Decimal' but the actual value was of type 'System.Double'. ---> System.InvalidCastException: Specified cast is not valid.

Stack trace:
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll
System.InvalidOperationException: An exception occurred while reading a database value. The expected type was 'System.Decimal' but the actual value was of type 'System.Double'. ---> System.InvalidCastException: Specified cast is not valid.
   at System.Data.SqlServerCe.SqlCeDataReader.GetDecimal(Int32 ordinal)
   at lambda_method(Closure , DbDataReader )
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
   at lambda_method(Closure , DbDataReader )
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.GetResult[TResult](IEnumerable`1 valueBuffers, Boolean throwOnNullResult)
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
   at TestHostInvalidCast.Tests.InvalidCastTest.<>c.<SqlCe_InvalidOperationException_Thrown>b__0_0(DataContext context) in D:\Projects\EfCoreRepro\InvalidCast\Tests\InvalidCastTest.cs:line 20

Steps to reproduce

Entity:

    public class InventoryPool
    {
        [Key]
        public int Id { get; set; }
        public double Quantity { get; set; }
    }

Query to reproduce error

context.Set<InventoryPool>().Sum(p => (decimal) p.Quantity);

Repro included at https://github.com/avinash-phaniraj-readify/EFCoreTestBed/tree/InvalidCast

Further technical details

EF Core version: 2.1.4
Database Provider: SqlServer and SqlCE
Operating system: Win 10
IDE: VS 2017 15.8.1

@ajcvickers
Copy link
Member

Note for triage: was able to easily repro this.

@optiks
Copy link

optiks commented Oct 11, 2018

@ajcvickers, if there’s any way of getting this into a 2.1 patch we’d be greatly appreciative. Totally understand if you can’t though.

@ajcvickers
Copy link
Member

@optiks At this point anything other than critical platform stability or security bugs will need to come through official paid support channels to be considered for a 2.1 patch.

@ajcvickers ajcvickers added this to the 2.2.0 milestone Oct 12, 2018
@bricelam
Copy link
Contributor

Dupe of #10642?

@ajcvickers ajcvickers modified the milestones: 2.2.0-preview3, 2.2.0 Oct 15, 2018
@smitpatel
Copy link
Contributor

This is regression introduced in #8861 unintentionally.

@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 Oct 16, 2018
smitpatel added a commit that referenced this issue Oct 16, 2018
We were losing convert nodes added by users and ended up getting results in wrong type

Resolves #13587
smitpatel added a commit that referenced this issue Oct 16, 2018
We were losing convert nodes added by users and ended up getting results in wrong type

Resolves #13587
@ajcvickers
Copy link
Member

ajcvickers commented Aug 10, 2019

Reopening because failing tests are disabled referencing this issue:

  • Using_DbSet_in_filter_works
  • Using_Context_set_method_in_filter_works
ystem.InvalidOperationException : Operation is not valid due to the current state of the object.
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateGroupBy(ShapedQueryExpression source, LambdaExpression keySelector, LambdaExpression elementSelector, LambdaExpression resultSelector) in C:\aspnet\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryableMethodTranslatingExpressionVisitor.cs:line 337
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\QueryableMethodTranslatingExpressionVisitor.cs:line 161
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in C:\aspnet\EntityFrameworkCore\src\EFCore.Relational\Query\RelationalQueryableMethodTranslatingExpressionVisitor.cs:line 74
   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) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\QueryCompilationContext.cs:line 68
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) in C:\aspnet\EntityFrameworkCore\src\EFCore\Storage\Database.cs:line 71
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 106
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0() in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 96
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 84
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 59
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\QueryCompiler.cs:line 92
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\EntityQueryProvider.cs:line 79
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator() in C:\aspnet\EntityFrameworkCore\src\EFCore\Query\Internal\EntityQueryable`.cs:line 94
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.GearsOfWarQueryTestBase`1.Include_collection_group_by_reference() in C:\aspnet\EntityFrameworkCore\test\EFCore.Specification.Tests\Query\GearsOfWarQueryTestBase.cs:line 5676
   at Microsoft.EntityFrameworkCore.Query.GearsOfWarQuerySqlServerTest.Include_collection_group_by_reference() in C:\aspnet\EntityFrameworkCore\test\EFCore.SqlServer.FunctionalTests\Query\GearsOfWarQuerySqlServerTest.cs:line 5563

@ajcvickers ajcvickers reopened this Aug 10, 2019
@ajcvickers ajcvickers removed this from the 2.2.0 milestone Aug 10, 2019
@ajcvickers
Copy link
Member

@smitpatel to follow up.

@smitpatel
Copy link
Contributor

Filed #17111

@smitpatel smitpatel added this to the 2.2.0 milestone Aug 12, 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. customer-reported regression type-bug
Projects
None yet
Development

No branches or pull requests

5 participants