Repository navigation
Render datetime2/datetimeoffset precision correctly in SQL Server ToQueryString - #39496
Conversation
…ueryString SQL Server type mappings store the fractional-seconds precision of datetime2, datetimeoffset and time parameters in DbParameter.Scale (that is what SqlClient sends to the server). TypeNameBuilder already read Scale for time, but read Precision for datetime2 and datetimeoffset, which is never set, so ToQueryString emitted e.g. "DECLARE @p datetime2 = '...1234567'" for a datetime2(3) parameter. The script then ran with 7 fractional digits instead of the 3 actually sent, which can change comparison results compared to the real query.
There was a problem hiding this comment.
🟡 Changes recommended
An active moderate finding shows that explicitly configured precision 0 is lost in generated SQL for both datetime2 and datetimeoffset parameters, causing scripts to differ from the executed query and requiring a fix with regression coverage.
1 open issue
What changed in this PR
This pull request updates SQL Server ToQueryString() parameter declarations so datetime2 and datetimeoffset use the precision stored in DbParameter.Scale, matching the precision sent to SQL Server.
Changes:
- Renders
datetime2anddatetimeoffsetprecision from parameter scale. - Adds regression coverage for
datetime2,datetimeoffset, andtime. - Refactors the test type-mapping setup for reuse.
This pull request requires changes.
| File | Description |
|---|---|
test/EFCore.SqlServer.Tests/Query/SqlServerQueryStringFactoryTest.cs |
Adds precision-aware query-string regression coverage and refactors type-mapping setup. |
src/EFCore.SqlServer/Query/Internal/SqlServerQueryStringFactory.cs |
Uses parameter scale for temporal type declarations; explicitly configured precision 0 is still omitted and requires correction. |
Review effort: Lite
ilkertskn
left a comment
There was a problem hiding this comment.
this is real: SqlClient tracks an explicitly set Scale = 0 via its internal HasScale flag and sends datetime2(0). However, that flag isn’t observable through DbParameter/SqlParameter’s public surface, and TypeNameBuilder only has the parameter (no type mapping), so there’s no clean way to distinguish explicit 0 from unset here without reflecting on SqlClient internals. The same limitation already existed for time(0) before this PR. This change fixes all non-zero precisions and doesn’t regress the zero case; I’d suggest tracking precision 0 separately if the team wants it handled.
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!

SQL Server type mappings store the fractional-seconds precision of datetime2, datetimeoffset and time parameters in DbParameter.Scale (that is what SqlClient sends to the server). TypeNameBuilder already read Scale for time, but read Precision for datetime2 and datetimeoffset, which is never set, so ToQueryString emitted e.g. "DECLARE @p datetime2 = '...1234567'" for a datetime2(3) parameter. The script then ran with 7 fractional digits instead of the 3 actually sent, which can change comparison results compared to the real query.