Skip to content

Parse scaffolded date and time default literals with the invariant culture in scaffolding - #38959

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
Dev-next-gen:scaffolding-date-defaults-invariant-culture
Sep 22, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
Dev-next-gen:scaffolding-date-defaults-invariant-culture

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • 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
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

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

While reading #35675, which made the scaffolders parse decimal and numeric default literals with CultureInfo.InvariantCulture (the da-DK report in #35654), I noticed that the date and time branches right next to it in SqliteDatabaseModelFactory.ParseClrDefaults and SqlServerDatabaseModelFactory.ParseClrDefault still call DateTime/DateOnly/TimeOnly/DateTimeOffset.TryParse(defaultValueSql, out ...) with the current culture.

With Gregorian cultures this goes unnoticed, but the current culture also brings its calendar. Under fa-IR (Persian calendar) the literal '1968-10-23' is read as year 1968 of that calendar, and the database model silently gets DefaultValue = 2590-01-12. Under ar-SA (Um Al-Qura) the parse fails and the default value is dropped, only DefaultValueSql remains. Literals in the T round-trip form such as '1973-09-03T12:00:01' parse the same way in every culture, so the existing tests would not catch it even on such a machine.

I ran the released SQLite scaffolder (EF Core 9.0.8, which has the same lines) against a database created with the SQL of the new test:

inv    A datetime        '1968-10-23 12:00:01'          -> DateTime 1968-10-23T12:00:01.0000000
inv    B date            '1968-10-23'                   -> DateOnly 1968-10-23
inv    C datetimeoffset  '1973-09-03 12:00:01+10:00'    -> DateTimeOffset 1973-09-03T12:00:01.0000000+10:00
fa-IR  A datetime        '1968-10-23 12:00:01'          -> DateTime 2590-01-12T12:00:01.0000000
fa-IR  B date            '1968-10-23'                   -> DateOnly 2590-01-12
fa-IR  C datetimeoffset  '1973-09-03 12:00:01+10:00'    -> DateTimeOffset 2594-11-23T12:00:01.0000000+10:00
ar-SA  A datetime        '1968-10-23 12:00:01'          -> null
ar-SA  B date            '1968-10-23'                   -> null
ar-SA  C datetimeoffset  '1973-09-03 12:00:01+10:00'    -> null

The change passes CultureInfo.InvariantCulture to those four calls in both factories, the same way the decimal branch already does. A literal that the invariant culture cannot parse keeps taking the existing path: no DefaultValue, the SQL stays in DefaultValueSql. I could not find a culture where the TimeOnly parse differs, I changed it anyway so the four branches stay consistent.

About the two unchecked boxes. There is no issue for this; it is the same class of bug as #35654, so I went straight to a PR, but I'm happy to open one if you prefer. And I don't have the .NET 11 SDK on my machines, so I could not run the test projects. What I did instead was compile the parsing branches taken verbatim from both files, before and after the change, into a .NET 8 harness, and run them under invariant, en-US, da-DK, fa-IR and ar-SA with the literals used by the tests: 16 wrong results before (all under fa-IR and ar-SA), none after. The SQLite run above used a real database. I have not run the SQL Server test against a server; its literals and DefaultValueSql expectations are the ones already asserted in Simple_DateTime_literals_are_parsed_for_HasDefaultValue and Simple_DateOnly_literals_are_parsed_for_HasDefaultValue.

AI tools used

…lture

- SqliteDatabaseModelFactory and SqlServerDatabaseModelFactory parsed DateTime, DateOnly, TimeOnly and DateTimeOffset default literals with the current culture. Under a culture whose calendar is not Gregorian, '1968-10-23' is read in that calendar: fa-IR scaffolds HasDefaultValue(2590-01-12), ar-SA silently drops the default.
- Pass CultureInfo.InvariantCulture, as dotnet#35675 already does for decimal and the other numeric literals.
- Add fa-IR tests next to the existing da-DK decimal ones.

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.

Copilot review overview

🟡 Changes recommended

The regression fixtures do not exercise temporal parsing correctly, and SQL Server DateTimeOffset coverage is incomplete.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 High severity

Open (2)
What changed in this PR

Updates SQL Server and SQLite scaffolding to parse temporal defaults with CultureInfo.InvariantCulture, adding Persian-culture regression tests.

Changes:

  • Applies invariant parsing to DateTime, DateOnly, TimeOnly, and DateTimeOffset.
  • Adds locale-specific scaffolding coverage.
  • Test fixtures require quoted temporal literals to exercise the parser correctly.
File Summary Review comments
test/​EFCore.SqlServer.FunctionalTests/​Scaffolding/​SqlServerDatabaseModelFactoryTest.cs Adds SQL Server culture regression coverage. Critical (3 votes): Temporal defaults are unquoted and cannot exercise the parser; quote literals and preserve quotes in DefaultValueSql.
test/​EFCore.Sqlite.FunctionalTests/​Scaffolding/​SqliteDatabaseModelFactoryTest.cs Adds SQLite culture regression coverage. Critical (3 votes): Defaults and insert values are unquoted; quote them and update expected SQL strings.
src/​EFCore.SqlServer/​Scaffolding/​Internal/​SqlServerDatabaseModelFactory.cs Uses invariant temporal parsing. Nit (1 vote): Add fa-IR coverage for an offset-bearing DateTimeOffset default.
src/​EFCore.Sqlite.Core/​Scaffolding/​Internal/​SqliteDatabaseModelFactory.cs Uses invariant temporal parsing. No final comments.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

- The parsers only reach the DateTime/DateOnly/DateTimeOffset branches
  after finding a single-quoted value; unquoted defaults were evaluated
  as arithmetic, not parsed as dates.
- Quote DEFAULT and INSERT literals, update DefaultValueSql expectations.

@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 changed the title Parse scaffolded date and time default literals with the invariant culture Parse scaffolded date and time default literals with the invariant culture in scaffolding Sep 22, 2026
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) September 22, 2026 21:52
@AndriySvyryd
AndriySvyryd merged commit 1f0ff12 into dotnet:main Sep 22, 2026
33 of 34 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 24, 2026
AndriySvyryd pushed a commit that referenced this pull request Sep 25, 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