StrEnum.Npgsql
2.0.0
Prefix Reserved
dotnet add package StrEnum.Npgsql --version 2.0.0
NuGet\Install-Package StrEnum.Npgsql -Version 2.0.0
<PackageReference Include="StrEnum.Npgsql" Version="2.0.0" />
<PackageVersion Include="StrEnum.Npgsql" Version="2.0.0" />
<PackageReference Include="StrEnum.Npgsql" />
paket add StrEnum.Npgsql --version 2.0.0
#r "nuget: StrEnum.Npgsql, 2.0.0"
#:package StrEnum.Npgsql@2.0.0
#addin nuget:?package=StrEnum.Npgsql&version=2.0.0
#tool nuget:?package=StrEnum.Npgsql&version=2.0.0
StrEnum.Npgsql
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.
Installation
Install StrEnum.Npgsql via the .NET CLI:
dotnet add package StrEnum.Npgsql
Usage
Defining a string enum
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");
}
Registering with the data source
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.
Using it
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>.
Acknowledgements
The wire-level type-info resolver is modelled directly on Npgsql.NetTopologySuite and Npgsql.Internal.Converters.EnumConverter.
License
Copyright © 2026 Dmytro Khmara.
StrEnum is licensed under the MIT license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
NuGet packages (1)
Showing the top 1 NuGet packages that depend on StrEnum.Npgsql:
| Package | Downloads |
|---|---|
|
StrEnum.Npgsql.EntityFrameworkCore
Entity Framework Core integration for StrEnum.Npgsql: maps StrEnum string enums to native Postgres enum types in EF Core models, migrations, and queries. |
GitHub repositories
This package is not used by any popular GitHub repositories.