Repository navigation
Keep fractional seconds in SQL Server TimeSpan literals - #39229
Merged
AndriySvyryd merged 2 commits intoOct 16, 2026
Merged
AndriySvyryd merged 2 commits into
AndriySvyryd merged 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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.
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
approved these changes
Oct 16, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

SqlServerTimeSpanTypeMapping decided whether to drop the fractional part by looking at TimeSpan.Milliseconds, which is wrong in both directions:
Check the fractional ticks that survive at the mapping's precision instead.