Skip to content

Add StrEnum.Npgsql.EntityFrameworkCore package#1

Merged
dmytro-khmara merged 1 commit intomasterfrom
add-strenum-npgsql-efcore-package
Apr 27, 2026
Merged

Add StrEnum.Npgsql.EntityFrameworkCore package#1
dmytro-khmara merged 1 commit intomasterfrom
add-strenum-npgsql-efcore-package

Conversation

@dmytro-khmara
Copy link
Copy Markdown
Member

Summary

Introduces StrEnum.Npgsql.EntityFrameworkCore — the EF Core wrapper around StrEnum.Npgsql 2.0. Maps StrEnum string enums to native Postgres enum types in EF Core models, migrations, and queries.

The split mirrors how the Npgsql ecosystem itself separates the ADO.NET driver from EF Core integration: raw-Npgsql consumers use StrEnum.Npgsql directly; EF Core consumers install this package on top.

What's in here

Public API (src/StrEnum.Npgsql.EntityFrameworkCore/):

  • DbContextOptionsBuilderExtensions.UseStringEnums() — installs the IValueConverterSelector that recognises StringEnum<T> properties as scalars (text-mode storage).
  • DbContextOptionsBuilderExtensions.UseStringEnumsAsPostgresEnums() — installs the new relational-type-mapping plugin so EF Core binds StringEnum<T> properties to native Postgres enum columns on the wire.
  • ModelBuilderExtensions.HasPostgresStringEnum<T>(name?, schema?) — declares the PG enum in the model (produces the CREATE TYPE migration).
  • ModelBuilderExtensions.MapStringEnumAsPostgresEnum<T>(name?, schema?) — declares the PG enum and configures the column type for every matching property.
  • PropertyBuilderExtensions.HasPostgresStringEnum<T>(name?, schema?) — per-property column-type configuration. Schema is inherited from the model-level call when omitted.

Internals (src/StrEnum.Npgsql.EntityFrameworkCore/Internal/):

  • NpgsqlStringEnumTypeMapping<T>RelationalTypeMapping whose ConfigureParameter pins NpgsqlParameter.DataTypeName to the PG enum name, so Npgsql's MapStringEnum<T> resolver picks up the parameter rather than EF defaulting to NpgsqlDbType.Text (the missing piece that produced 42804: column is of type my_enum but expression is of type text end-to-end).
  • NpgsqlStringEnumTypeMappingSourcePluginIRelationalTypeMappingSourcePlugin returning the mapping for any StringEnum<T> property with an explicit store type.
  • StrEnumNpgsqlOptionsExtensionIDbContextOptionsExtension wiring the plugin into EF Core's service collection via UseStringEnumsAsPostgresEnums().

Tests:

  • 23 unit tests covering the value-converter selector trio, the model/property builder extensions (schema inheritance, name/schema overrides, etc.), and the type-mapping plugin's FindMapping shape.
  • 2 Testcontainers-backed integration tests that round-trip StringEnum<T> properties through EF Core against a real Postgres — both default and schema-qualified PG enum types. These also serve as the proof that UseStringEnumsAsPostgresEnums() closes the wire-binding gap that the previous resolver-only attempt couldn't reach from inside EF Core.

CI: mirrors the structure used by StrEnum.Npgsql — Docker-based unit-test job plus a separate integration-tests job using Docker on the GitHub runner for Testcontainers.

Test plan

  • dotnet test — 23 unit + 2 integration pass on net10.0
  • Build against StrEnum.Npgsql 2.0.0 (just published) via PackageReference, not a relative ProjectReference
  • Smoke-test from a fresh consumer app once published (text mode + PG enum mode + a query with a WHERE x = T.Value predicate)

@dmytro-khmara dmytro-khmara merged commit fa5623d into master Apr 27, 2026
2 checks passed
@dmytro-khmara dmytro-khmara deleted the add-strenum-npgsql-efcore-package branch April 27, 2026 17:53
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