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

Query: InvalidCastExpression when inferred type mapping fails to print literal #15948

Closed
smitpatel opened this issue Jun 4, 2019 · 0 comments · Fixed by #15954
Closed

Query: InvalidCastExpression when inferred type mapping fails to print literal #15948

smitpatel opened this issue Jun 4, 2019 · 0 comments · Fixed by #15954
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

@smitpatel
Copy link
Contributor

public virtual async Task Can_filter_projection_with_inline_enum_variable(bool async)
        {
            using (var context = CreateContext())
            {
                var query = context
                    .Set<EmailTemplate>()
                    .Select(
                        t => new EmailTemplateDto
                        {
                            Id = t.Id,
                            TemplateType = (EmailTemplateTypeDto)t.TemplateType
                        })
                    .Where(t => t.TemplateType == EmailTemplateTypeDto.PasswordResetRequest);

                var results = async
                    ? await query.ToListAsync()
                    : query.ToList();

                Assert.Equal(1, results.Count);
                Assert.Equal(EmailTemplateTypeDto.PasswordResetRequest, results.Single().TemplateType);
            }
        }

For predicate in this case, we never see enum constant. It is always converted to int. And then we use type mapping from the other side to print out the int which throws

Message: 
    System.InvalidCastException : Invalid cast from 'System.Int32' to 'Microsoft.EntityFrameworkCore.BuiltInDataTypesTestBase`1+EmailTemplateTypeDto[[Microsoft.EntityFrameworkCore.ConvertToProviderTypesSqlServerTest+ConvertToProviderTypesSqlServerFixture, Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60]]'.
  Stack Trace: 
    at Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
    at IConvertible.ToType(Type type, IFormatProvider provider)
    at Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at Convert.ChangeType(Object value, Type conversionType)
    at ValueConverter`2.Sanitize[T](Object value) in ValueConverter`.cs line: 52
    at <>c__DisplayClass3_0`2.<SanitizeConverter>b__0(Object v) in ValueConverter`.cs line: 43
    at RelationalTypeMapping.GenerateSqlLiteral(Object value) in RelationalTypeMapping.cs line: 503
    at QuerySqlGenerator.VisitSqlConstant(SqlConstantExpression sqlConstantExpression) in QuerySqlGenerator.cs line: 318
    at SqlExpressionVisitor.VisitExtension(Expression extensionExpression) in SqlExpressionVisitor.cs line: 58
    at QuerySqlGenerator.VisitSqlBinary(SqlBinaryExpression sqlBinaryExpression) in QuerySqlGenerator.cs line: 298
    at SqlExpressionVisitor.VisitExtension(Expression extensionExpression) in SqlExpressionVisitor.cs line: 52
    at QuerySqlGenerator.VisitSelect(SelectExpression selectExpression) in QuerySqlGenerator.cs line: 111
    at QuerySqlGenerator.GetCommand(SelectExpression selectExpression) in QuerySqlGenerator.cs line: 57
    at Enumerator.MoveNext() in QueryingEnumerable.cs line: 101
    at List`1.ctor(IEnumerable`1 collection)
    at Enumerable.ToList[TSource](IEnumerable`1 source)
    at BuiltInDataTypesTestBase`1.Can_filter_projection_with_inline_enum_variable(Boolean async) in BuiltInDataTypesTestBase.cs line: 72
    at --- End of stack trace from previous location where exception was thrown ---
@smitpatel smitpatel added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug labels Jun 4, 2019
@smitpatel smitpatel self-assigned this Jun 4, 2019
@smitpatel smitpatel added this to the 3.0.0 milestone Jun 5, 2019
smitpatel added a commit that referenced this issue Jun 5, 2019
…st in generated SQL

Enable BuiltInDataTypeTests
- Preserve convert nodes around parameters/constants in parameter extracting.
- While translating to Sql, remove implicit convert nodes. Implicit convert nodes appear only for binary expressions since equality operators are not defined for all types
- Apply explicit cast in SQL only when converted type is mapped.
- Convert int value to enum value before printing literal (how old pipeline did it)

Resolves #14159
Resolves #15330
Resolves #15948
smitpatel added a commit that referenced this issue Jun 5, 2019
…st in generated SQL

Enable BuiltInDataTypeTests
- Preserve convert nodes around parameters/constants in parameter extracting.
- While translating to Sql, remove implicit convert nodes. Implicit convert nodes appear only for binary expressions since equality operators are not defined for all types
- Apply explicit cast in SQL only when converted type is mapped.
- Convert int value to enum value before printing literal (how old pipeline did it)

Resolves #14159
Resolves #15330
Resolves #15948
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview7 Jul 2, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview7, 3.0.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
2 participants