Skip to content

Convert unsigned enums to decimal through their underlying type - #39228

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/enum-to-decimal-converter-unsigned
Oct 15, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/enum-to-decimal-converter-unsigned

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

EnumToNumberConverter<TEnum, decimal> converted the enum through long, so values of a ulong-backed enum above long.MaxValue were written as negative decimals (ulong.MaxValue became -1) and reading them back threw an OverflowException, even though the default mapping hints ask for decimal(20, 0) precisely to hold the full ulong range. Convert through the enum's underlying type 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

EnumToNumberConverter<TEnum, decimal> converted the enum through long, so
values of a ulong-backed enum above long.MaxValue were written as negative
decimals (ulong.MaxValue became -1) and reading them back threw an
OverflowException, even though the default mapping hints ask for
decimal(20, 0) precisely to hold the full ulong range. Convert through the
enum's underlying type instead.
@ilkertskn
ilkertskn requested a review from a team as a code owner October 12, 2026 19:19
…tored as -1

JsonTypesRelationalTestBase overrode the ulong enum JSON tests to expect -1
for EnumU64.Max ("because ulong is converted to long on SQL Server"), which
was the EnumToNumberConverter<TEnum, decimal> bug fixed in the previous commit.
SQL Server now writes 18446744073709551615 like the other providers, so the
overrides are removed, along with the SQLite overrides that only restored the
base expectations.
@ilkertskn

Copy link
Copy Markdown
Contributor Author

The SQL Server failures came from JsonTypesRelationalTestBase, which overrode the ulong enum JSON tests to expect -1 for EnumU64.Max (“because ulong is converted to long on SQL Server”). That expectation encoded the bug this PR fixes. I removed those overrides (and the SQLite ones that only restored the base expectations), so SQL Server now uses the same 18446744073709551615 expectation as the other providers. The full SQL Server functional suite passes locally. The Cosmos (ubuntu-24.04) failure looks unrelated: the same job is failing on other recent PRs too.

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

No unresolved review issues remain, and the changes include appropriate boundary coverage.

0 open findings

What changed in this PR

Fixes enum-to-decimal conversion for unsigned underlying types, preserving values above long.MaxValue.

Changes:

  • Convert through the enum’s underlying type.
  • Add ulong boundary conversion tests.
  • Remove obsolete provider-specific JSON overrides.
File Description
test/​EFCore.Tests/​Storage/​ValueConversion/​EnumToNumberConverterTest.cs Adds unsigned decimal conversion boundary tests.
test/​EFCore.Sqlite.FunctionalTests/​JsonTypesSqliteTest.cs Removes obsolete overrides.
test/​EFCore.Relational.Specification.Tests/​JsonTypesRelationalTestBase.cs Removes obsolete negative-value expectations.
src/​EFCore/​Storage/​ValueConversion/​EnumToNumberConverter.cs Converts through enum underlying types.

🧠 Review effort: Lite


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

@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 0a3695a into dotnet:main Oct 15, 2026
19 checks passed
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