Skip to content

Keep fractional seconds in SQL Server TimeSpan literals - #39229

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/sqlserver-timespan-literal-sub-millisecond
Oct 16, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/sqlserver-timespan-literal-sub-millisecond

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

SqlServerTimeSpanTypeMapping decided whether to drop the fractional part by looking at TimeSpan.Milliseconds, which is wrong in both directions:

  • A value with sub-millisecond ticks only (e.g. 13:10:15.00002) has Milliseconds == 0, so the literal silently became '13:10:15' even for time(7), losing data in seed data, default values and inlined constants.
  • With a precision below 3, a value whose remaining digits are zero (e.g. 50 ms for time(1)) produced '13:10:15.', which SQL Server rejects.

Check the fractional ticks that survive at the mapping's precision instead.

  • [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

SqlServerTimeSpanTypeMapping decided whether to drop the fractional part by
looking at TimeSpan.Milliseconds, which is wrong in both directions:

- A value with sub-millisecond ticks only (e.g. 13:10:15.00002) has
  Milliseconds == 0, so the literal silently became '13:10:15' even for
  time(7), losing data in seed data, default values and inlined constants.
- With a precision below 3, a value whose remaining digits are zero
  (e.g. 50 ms for time(1)) produced '13:10:15.', which SQL Server rejects.

Check the fractional ticks that survive at the mapping's precision instead.

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

The critical converter compatibility issue in GenerateSqlLiteral remains unresolved.

1 open finding
What changed in this PR

This PR fixes SQL Server TimeSpan literal generation for fractional precision and low-precision mappings.

Changes:

  • Preserves sub-millisecond fractional ticks.
  • Avoids invalid trailing decimal separators.
  • Adds regression coverage for affected precision cases.
File Summary
test/​EFCore.SqlServer.Tests/​Storage/​SqlServerTypeMappingTest.cs Adds regression tests for fractional and precision-specific literals.
src/​EFCore.SqlServer/​Storage/​Internal/​SqlServerTimeSpanTypeMapping.cs Updates precision-aware literal formatting; converter compatibility requires correction.

🧠 Review effort: Lite

…iterals

GenerateSqlLiteral applies the mapping's value converter before calling
GenerateNonNullSqlLiteral, so the value isn't necessarily a TimeSpan (e.g. with
a TimeSpanToStringConverter composed onto the mapping). Fall back to the
previous formatting in that case instead of throwing InvalidCastException.
@ilkertskn

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. When a converter composed onto the mapping produces a non-TimeSpan provider value (e.g. TimeSpanToStringConverter), GenerateNonNullSqlLiteral now falls back to the previous formatting instead of casting. I also added a regression test for a converter-backed mapping.

@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 eae5924 into dotnet:main Oct 16, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 20, 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