Lets you map StrEnum string enums to native Postgres enum types via the Npgsql ADO.NET driver — analogous to what MapEnum does for regular C# enums.
For Entity Framework Core integration, install StrEnum.Npgsql.EntityFrameworkCore, which adds model-level registration and migrations on top of this package.
Supports Npgsql 8 – 10. Targets net8.0, net9.0, net10.0.
Install StrEnum.Npgsql via the .NET CLI:
dotnet add package StrEnum.Npgsql
public class Sport : StringEnum<Sport>
{
public static readonly Sport RoadCycling = Define("ROAD_CYCLING");
public static readonly Sport MountainBiking = Define("MTB");
public static readonly Sport TrailRunning = Define("TRAIL_RUNNING");
}Assuming the Postgres enum type already exists in the database (CREATE TYPE sport AS ENUM ('ROAD_CYCLING', 'MTB', 'TRAIL_RUNNING')), call MapStringEnum<TEnum>() on the data source builder to teach Npgsql how to bind it on the wire:
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.MapStringEnum<Sport>(); // public.sport
dataSourceBuilder.MapStringEnum<Sport>("sport_kind", "races"); // races.sport_kind
await using var dataSource = dataSourceBuilder.Build();MapStringEnum<TEnum>() mirrors the shape of Npgsql's built-in MapEnum<TEnum>() for regular C# enums, and registers a PgTypeInfoResolverFactory that maps Sport ↔ the named Postgres enum's OID.
Bind a Sport instance to a parameter — Npgsql sends it as the enum type:
await using var insert = dataSource.CreateCommand();
insert.CommandText = "INSERT INTO races (id, name, sport) VALUES ($1, $2, $3)";
insert.Parameters.AddWithValue(Guid.NewGuid());
insert.Parameters.AddWithValue("Cape Epic");
insert.Parameters.AddWithValue(Sport.MountainBiking);
await insert.ExecuteNonQueryAsync();Read it back the same way — values come out as Sport instances:
await using var select = dataSource.CreateCommand();
select.CommandText = "SELECT sport FROM races WHERE id = $1";
select.Parameters.AddWithValue(raceId);
var sport = (Sport)(await select.ExecuteScalarAsync())!;MapStringEnum<TEnum>() is also available on INpgsqlTypeMapper (for use with NpgsqlConnection.GlobalTypeMapper or other type-mapper implementations) and on the slim data source builder — same overload shape as Npgsql's MapEnum<TEnum>.
The wire-level type-info resolver is modelled directly on Npgsql.NetTopologySuite and Npgsql.Internal.Converters.EnumConverter.
Copyright © 2026 Dmytro Khmara.
StrEnum is licensed under the MIT license.