MSSQL (Microsoft.Data.SqlClient): can't retrieve DateOnly
See original GitHub issueI’m using .net 6, SQL Hydra v. 1.0.0, and Microsoft.Data.SqlClient v. 4.1.0 to connect to an MSSQL database that includes some DATE
columns. SQL Hydra tries to use System.DateOnly
for this, and it can write dates to the database just fine, but as soon as it tries to read a date from the database, it crashes in the SqlHydra-generated Reader code with
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.DateOnly'.
at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueFromSqlBufferInternal[T](SqlBuffer data, _SqlMetaData metaData, Boolean isAsync)
at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValueInternal[T](Int32 i, Boolean isAsync)
at Microsoft.Data.SqlClient.SqlDataReader.GetFieldValue[T](Int32 i)
at MyProject.MyDatabase.dbo.get_MyColumn@537-2.Invoke(Int32 arg00) in C:\Git\MyRepo\MyProject\MyDatabase.fs:line 537
at MyProject.MyDatabase.RequiredColumn`2.Read(FSharpOption`1 alias) in C:\Git\MyRepo\MyProject\MyDatabase.fs:line 11
at MyProject.MyDatabase.dbo.MyTableReader.Read() in C:\Git\MyRepo\MyProject\MyDatabase.fs:line 542
It seems like GetFieldValue<DateOnly>
doesn’t work? Can we just use GetDateTime >> DateOnly.FromDateTime
?
Issue Analytics
- State:
- Created a year ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
.NET 6 | Support the new BCL DateOnly and TimeOnly ...
NET 6 as alternatives to DateTime (dotnet/runtime#49036). ... MSSQL (Microsoft.Data.SqlClient): can't retrieve DateOnly JordanMarr/SqlHydra# ...
Read more >c# - Date Only cannot be mapped SQL Server 2019
I am trying to use the new DateOnly aspects of c# but when I come to do my migrations I am having the...
Read more >Using .NET 6 DateOnly (and TimeOnly) with SQL Server
Trying out the new support in SqlClient If you do not use the updated package, you will get an error message similar to:...
Read more >Introduction to Microsoft.Data.SqlClient namespace
Learn about the Microsoft.Data.SqlClient namespace and how it's the preferred way to connect to SQL for .NET applications.
Read more >Support for DateOnly/TimeOnly for SQL Server has been ...
It was supported for some providers, EF SQL Server had a dependency on Microsoft.Data.SqlClient to support it first.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the really fast turnaround on this, and for the awesome library for working with databases! Really a big help!
That fix definitely works:
So the better interim fix would be for you to manually add that fix to your generated file (rather than reverting to net5) until I release the patch.
Also change it at the bottom of the generated file as well (for doing
select
on individual columns):