SqlClient: Microsoft.Data.SqlClient.SqlConnection is converting DateTime objects to datetime2 columns incorrectly.
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When using Microsoft.Data.SqlClient.SqlConnection.QuerySingleAsync to call a sql insert query, DateTime objects are not maintaining their millisecond precision when being inserted into a datetime2 column, instead reverting to the old datetime behaviour of only storing the milliseconds ending with 000, 003, or 007.
Inserting the record directly via raw sql query works as expected:
Note the highlighted value and column match with ending digits 222.
And now, from the following code:
using Microsoft.Data.SqlClient.SqlConnection connection = DatabaseConfig.GetConnection();
var result = await connection.QuerySingleAsync<UserDatabaseModel>(insertPackage.sql, insertPackage.parameters)
Let’s look at the query and parameters:
Please note the Immediate window showing the sql query being sent into System.Data.SqlClient.SqlConnection’s QuerySingleAsync function.
Then please refer to the highlighted items in the local windows that correspond to the @lockout_ends_at parameter. The milliseconds are 222 as they should be.
I step forward and now the value returned by the database is incorrect:
The value in the database is also incorrect.
The only thing that occurred between my correct value and the incorrect value is the DateTime object in question being converted to a value to place into my datetime2(7) column. It should have preserved 222. It did not.
Expected Behavior
Since the columns are a datetime2 and not an original datetime, millisecond precision should be preserved.
Steps To Reproduce
- Use a General Use Azure Serverless Sql Server Database.
- Use .NET Core 7
- Use the code in the above example to insert a parameterized query.
Exceptions (if any)
No response
.NET Version
7.0.304
Anything else?
Microsoft.NETCore.App v7.0.7
dotnet --info
Version: 7.0.304
Commit: 7e794e2806
Runtime Environment:
OS Name: Mac OS X
OS Version: 13.3
OS Platform: Darwin
RID: osx.13-arm64
Base Path: /usr/local/share/dotnet/sdk/7.0.304/
Host:
Version: 7.0.7
Architecture: arm64
Commit: 5b20af47d9
.NET SDKs installed:
6.0.410 [/usr/local/share/dotnet/sdk]
7.0.304 [/usr/local/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.18 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.18 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 16 (8 by maintainers)
A team-member with sufficient rights can move this issue over there. At the moment you just have to wait 😉
@JRahnama Yep here you go:
BrokenDateTime.zip
Just run the sln and it’ll output the issue:
@gfoidl @martincostello Thanks to both of you! I’ll stay tuned.
This issue looks like it should be transferred to https://github.com/dotnet/SqlClient.