Skip to content

Update StrEnum to v2.1.0 and add integration tests for string enums in composite primary keys - #4

Merged
dmytro-khmara merged 3 commits into
masterfrom
composite-primary-key-support
Aug 13, 2026
Merged

Update StrEnum to v2.1.0 and add integration tests for string enums in composite primary keys#4
dmytro-khmara merged 3 commits into
masterfrom
composite-primary-key-support

Conversation

@dmytro-khmara

Copy link
Copy Markdown
Member

Summary

  • Updates StrEnum to v2.1.0, which adds IComparable<TEnum>, IComparable, and IEquatable<TEnum> to StringEnum<TEnum>. The floor moves to [2.1.0,3.0.0) because string enums in keys don't work without those interfaces.
  • Adds CompositePrimaryKeyTests, covering a string enum as part of a composite primary key.

This is the scenario the StrEnum change was made for. A string enum mapped to a native Postgres enum has no value converter, so EF Core compares key values using the model CLR type. EF orders key values internally and rejects key types that aren't comparable, so on StrEnum 2.0.x the model below fails validation:

Property 'NationalRecord.Sport' cannot be used as a key because it has type 'Sport' which does not implement 'IComparable<T>', 'IComparable' or 'IStructuralComparable'. Use 'HasConversion' in 'OnModelCreating' to wrap 'Sport' with a type that can be compared.

The suggested HasConversion workaround changes the mapped storage type, which is awkward when the column is a native Postgres enum — you then need an explicit HasColumnType plus a cast on the raw-SQL parameter path. Taking StrEnum 2.1.0 avoids all of that.

The new tests key NationalRecord on (Sport, Year) and cover the model shape (composite key, no value converter, sport column type), a full round-trip through insert, FindAsync on the composite key, update and query, and two rows that share a year but differ only by the enum.

Test plan

  • Confirmed the tests fail for the right reason: pinned back to StrEnum 2.0.3, all three fail with the cannot be used as a key error above; they pass on 2.1.0
  • dotnet test — 24 unit and 5 integration tests pass against Postgres 16 via Testcontainers
  • No Dockerfile or workflow change needed: the Docker test target covers the unit tests, and the integration-tests job runs the integration project as a whole

@dmytro-khmara
dmytro-khmara merged commit c22e23a into master Aug 13, 2026
2 checks passed
@dmytro-khmara
dmytro-khmara deleted the composite-primary-key-support branch August 13, 2026 21:45
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.

1 participant