Skip to content

Use the invariant culture for small and unsigned integer literals in CSharpHelper - #39319

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-small-integer-literal-invariant-culture
Oct 21, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-small-integer-literal-invariant-culture

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

Literal(int) and Literal(long) format with the invariant culture, but the sbyte/short/byte/ushort/uint/ulong overloads concatenated the value directly, which uses the current culture. In cultures whose negative sign is not ASCII (e.g. sv-SE, nb-NO, fi-FI use U+2212) negative sbyte/short values - such as HasDefaultValue((short)-1), seed data, or unnamed sbyte/short enum values - were generated as "(short)−1", which does not compile in the generated snapshot, migration or scaffolded code.

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

…CSharpHelper

Literal(int) and Literal(long) format with the invariant culture, but the
sbyte/short/byte/ushort/uint/ulong overloads concatenated the value directly,
which uses the current culture. In cultures whose negative sign is not ASCII
(e.g. sv-SE, nb-NO, fi-FI use U+2212) negative sbyte/short values - such as
HasDefaultValue((short)-1), seed data, or unnamed sbyte/short enum values -
were generated as "(short)−1", which does not compile in the generated
snapshot, migration or scaffolded code.

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.

🟢 Approval recommended

The changes address culture-dependent literal generation and include targeted regression coverage.

No open issues 🎉

What changed in this PR

This pull request updates CSharpHelper to generate culture-independent literals for small and unsigned integer types, preventing invalid generated C# under cultures with non-ASCII negative signs.

Changes:

  • Applies CultureInfo.InvariantCulture to byte, sbyte, short, ushort, uint, and ulong literal formatting.
  • Adds Swedish-culture regression coverage for negative integer and unnamed enum literals.
File Description
test/​EFCore.Design.Tests/​Design/​Internal/​CSharpHelperTest.cs Verifies generated literals remain valid under sv-SE, including negative values and enums.
src/​EFCore.Design/​Design/​Internal/​CSharpHelper.cs Applies invariant-culture formatting to the affected integer literal overloads.

Review effort: Lite

@AndriySvyryd
AndriySvyryd merged commit 94221a9 into dotnet:main Oct 21, 2026
20 checks passed
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

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