You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.TimeSpan'.'
at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueFromSqlBufferInternal[T](SqlBuffer data, _SqlMetaData metaData, Boolean isAsync)
at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValue[T](Int32 i)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ScratchDb.Program.Main(String[] args) in Program.cs:line 19
Version information
EF Core version: 3.1.9 and 7.0.1 tested
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0, .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2022 17.3.6
Generated query
SELECT [t].[Key], [t].[Key0], [t].[Key1], [t].[Key2], [t].[Key3]
FROM (
SELECT DATEPART(year, [f].[Date]) AS [Key], DATEPART(month, [f].[Date]) AS [Key0], 1AS [Key1], 0AS [Key2], '00:00:00'AS [Key3]
FROM [Widgets] AS [f]
) AS [t]
GROUP BY [t].[Key], [t].[Key0], [t].[Key1], [t].[Key2], [t].[Key3]
As we can see in the generated query, [Key3] is '00:00:00', which fails to be correctly converted to a TimeSpan.
Note that without the GroupBy, the generated query is different, and '00:00:00' is not in there, thus not triggering this issue:
SELECT [t].[Year], [t].[Month]
FROM (
SELECT DATEPART(year, [f].[Date]) AS [Year], DATEPART(month, [f].[Date]) AS [Month]
FROM [Widgets] AS [f]
) AS [t]
GROUP BY [t].[Year], [t].[Month]
Which, again, does not include a '00:00:00' string, and thus works fine.
The text was updated successfully, but these errors were encountered:
Code to reproduce issue:
Exception thrown
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.TimeSpan'.'
Version information
EF Core version: 3.1.9 and 7.0.1 tested
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0, .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2022 17.3.6
Generated query
As we can see in the generated query,
[Key3]
is '00:00:00', which fails to be correctly converted to aTimeSpan
.Note that without the
GroupBy
, the generated query is different, and '00:00:00' is not in there, thus not triggering this issue:Generates this query:
Workaround
By using an anonymous class instead of a
DateTimeOffset
in theGroupBy
, the issue disappears:Generates this query:
Which, again, does not include a '00:00:00' string, and thus works fine.
The text was updated successfully, but these errors were encountered: