Skip to content

Keep unnamed bits when generating [Flags] enum literals in CSharpHelper - #39381

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-flags-enum-unnamed-bits
Oct 22, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-flags-enum-unnamed-bits

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

A flags value with bits that have no named member (e.g. (E)5 where only One = 1 is named) was generated as E.One, silently dropping the remaining bits in migrations, snapshots and compiled models.

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

A flags value with bits that have no named member (e.g. (E)5 where only
One = 1 is named) was generated as E.One, silently dropping the remaining
bits in migrations, snapshots and compiled models.

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 reviewed changes address the reported data-loss issue and include regression coverage.

No open issues 🎉

What changed in this PR

This pull request updates CSharpHelper to preserve unnamed bits when generating literals for [Flags] enum values, preventing data loss in migrations, snapshots, and compiled models.

Changes:

  • Emits unnamed flag bits as an explicit enum cast.
  • Handles fully named, fully unnamed, and partially unnamed flag values.
  • Adds regression test coverage for these scenarios.
File Description
test/​EFCore.Design.Tests/​Design/​Internal/​CSharpHelperTest.cs Adds regression tests for flags values containing unnamed bits.
src/​EFCore.Design/​Design/​Internal/​CSharpHelper.cs Preserves unnamed bits in generated flags enum expressions.

Review effort: Lite

@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 f389bd5 into dotnet:main Oct 22, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 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