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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StrEnum.Npgsql" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrEnum.Npgsql" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="StrEnum.Npgsql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StrEnum.Npgsql --version 2.0.0
                    
#r "nuget: StrEnum.Npgsql, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StrEnum.Npgsql@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StrEnum.Npgsql&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=StrEnum.Npgsql&version=2.0.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

  • net8.0

  • net9.0

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.

Version Downloads Last Updated
2.0.0 166 4/26/2026
1.0.1 93 4/25/2026
1.0.0 90 4/25/2026