-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Nullable DateTime in projection class - System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.' #12797
Comments
I was able to reproduce this on current bits - model needs to contain TicketNumber entity without any TicketNumberDeltas, so that COALESCE tries to return the second argument) |
Bug was introduced when fixing: #10294. Before this scenario would throw a different exception - nullable object must have a value |
@brunolau workaround is to cast the var query = db.TicketNumbers.Where(p => p.Id == 3422).Select(p => new ProjectionClass
{
Id = p.Id,
DeltaCount = p.TicketNumberDeltas.Count(),
LastEntry = p.TicketNumberDeltas.OrderByDescending(lx => lx.RecordTimeStamp).Select(le => (DateTime?)le.RecordTimeStamp).FirstOrDefault()
}).FirstOrDefault(); |
Workaround works, thank you! |
Actually, fix for #10294 only exposed this issue for this particular scenario - I was able to reproduce it without the aggregate. It could still be a regression but need to investigate the underlying cause to determine this. |
@maumar Let's investigate for patch. |
See also #12819 when working on this. |
@maumar Any update on this? |
A potentially simpler reproduction is this. var ret = from t in context.NullableDate Code worked fine in Ef Core 2.0 |
confirmed that scenario provided by @ksmithRenweb is a regression from 2.0 |
in 2.0 we used to generate: SELECT COALESCE([t].[NullableDate], '1900-01-01T00:00:00.000') AS [NotNullable]
FROM [Nullables] AS [t] and now we generate: SELECT COALESCE([t].[NullableDate], '1900-01-01T00:00:00.0000000') AS [NotNullable]
FROM [Nullables] AS [t] |
@ksmithRenweb - What is the data type of Column |
|
we should be using the type from the other side of coalesce to determine how to render a datetime literal for the second term. We have a general issue to be better about it, but perhaps there is something we can easily do for this specific case. @smitpatel thoughts? |
Looks like, our type mapping inference for Binary is missing out Coalesce by accident. Should be easy fix. |
@smitpatel Data Type of "datetime" and of course set to Allow Nulls. |
@ajcvickers to give this a read before next triage |
Notes from triage: The literal generator for the provider/type/value combination should throw if asked to generate a literal for values that cannot be represented in the store. Se also #12998. |
Removing from milestone to discuss in triage. Not sure what is left to do here. |
@smitpatel to create a repro. |
In 3.0 query pipeline we reverted the fix we made for #10294 which introduced Coalesce function in sql tree. Now we let server side be null and when reading back valuetype from it, get default value. Hence we don't generate problematic SQL anymore. |
Having a one to many relationship, the main entity can have zero to n child entities. When I want to select the main entity's Id and most recent DateTime from the child entity collection, SQL execution crashes with a System.Data.SqlClient.SqlException
Steps to reproduce
EfCoreErrReproduction.zip
Further technical details
EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10 Enterprise x64
IDE: Visual Studio 2017 15.7.5
The text was updated successfully, but these errors were encountered: