Repository navigation
Render unnamed negative enum values correctly in LinqToCSharpSyntaxTranslator - #39497
Merged
AndriySvyryd merged 1 commit intoNov 3, 2026
Merged
Conversation
…anslator An unknown enum value is formatted by Enum.Format as a number, which was only detected when it started with a digit. A negative value such as (MyEnum)(-1000) was therefore emitted as the member access MyEnum.-1000, which doesn't compile. Detect any non-identifier formatting, read the value via Convert instead of re-parsing the string, and parenthesize negative literals, since (MyEnum)-1000L is parsed as a subtraction.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified, and the described implementation and regression coverage address negative signed and large unsigned unnamed enum values.
No open issues 🎉
What changed in this PR
This pull request fixes C# generation for unnamed negative and unsigned enum values in LinqToCSharpSyntaxTranslator, ensuring the emitted casts are valid and preserve the underlying values.
Changes:
- Detects non-identifier enum formatting, including negative values.
- Converts enum values directly using their signed or unsigned underlying representation.
- Parenthesizes negative literals to avoid invalid cast syntax.
- Adds regression tests for negative signed and large unsigned enum values.
| File | Description |
|---|---|
test/EFCore.Design.Tests/Query/LinqToCSharpSyntaxTranslatorTest.cs |
Adds coverage for negative unnamed values and large unsigned enum values. |
src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs |
Generates valid casts for unnamed signed and unsigned enum values. |
Review effort: Lite
AndriySvyryd
approved these changes
Nov 3, 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.
An unknown enum value is formatted by Enum.Format as a number, which was only detected when it started with a digit. A negative value such as (MyEnum)(-1000) was therefore emitted as the member access MyEnum.-1000, which doesn't compile. Detect any non-identifier formatting, read the value via Convert instead of re-parsing the string, and parenthesize negative literals, since (MyEnum)-1000L is parsed as a subtraction.