question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MSSQL (Microsoft.Data.SqlClient): can't retrieve DateOnly

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ntwilsoncommented, Jul 15, 2022

Thanks for the really fast turnaround on this, and for the awesome library for working with databases! Really a big help!

1reaction
JordanMarrcommented, Jul 15, 2022

That fix definitely works:

image

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):

image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found