Skip to content

Render datetime2/datetimeoffset precision correctly in SQL Server ToQueryString - #39496

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-query-string-datetime2-precision
Nov 4, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-query-string-datetime2-precision

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

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.

  • [ x] I've read the guidelines for contributing and seen the walkthrough
  • [x ] I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [ x] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [x ] Tests for the changes have been added (for bug fixes / features)
  • [ x] Code follows the same patterns and style as existing code in this repo

…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 datetime2 and datetimeoffset precision from parameter scale.
  • Adds regression coverage for datetime2, datetimeoffset, and time.
  • 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 ilkertskn left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit f172586 into dotnet:main Nov 4, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants