Skip to content

Fix mapping of enums with duplicate labels#3743

Merged
roji merged 1 commit intonpgsql:mainfrom
roji:DuplicateEnumValues
Feb 20, 2026
Merged

Fix mapping of enums with duplicate labels#3743
roji merged 1 commit intonpgsql:mainfrom
roji:DuplicateEnumValues

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Feb 20, 2026

Fixes #3742

Copilot AI review requested due to automatic review settings February 20, 2026 08:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #3742 where mapping an enum with members that have the same value (duplicate labels) throws an ArgumentException. The solution allows enums with duplicate values by using the first label encountered, while ensuring that all duplicate values with [PgName] attributes have consistent mappings.

Changes:

  • Modified EnumDefinition constructor to handle duplicate enum values gracefully by keeping only the first label encountered
  • Added validation to throw InvalidOperationException when duplicate enum values have conflicting [PgName] attribute mappings
  • Added comprehensive unit tests covering four scenarios: duplicate values without attributes, with matching attributes, with different attributes, and with mixed attribute usage

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/EFCore.PG/Infrastructure/Internal/EnumDefinition.cs Replaced simple ToDictionary with explicit loop that handles duplicate enum values and validates [PgName] consistency
test/EFCore.PG.Tests/Infrastructure/EnumDefinitionTest.cs Added new test file with four test cases covering all duplicate enum value scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roji roji enabled auto-merge (squash) February 20, 2026 08:10
@roji roji merged commit 7b0887f into npgsql:main Feb 20, 2026
14 checks passed
@roji roji deleted the DuplicateEnumValues branch February 20, 2026 08:13
roji added a commit that referenced this pull request Feb 20, 2026
@roji
Copy link
Copy Markdown
Member Author

roji commented Feb 20, 2026

Backported to 10.0.1 via 87ead6e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mapping an Enum where members have the same value throws ArgumentException: An item with the same key has already been added.

2 participants