StrEnum.Npgsql 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package StrEnum.Npgsql --version 1.0.0
                    
NuGet\Install-Package StrEnum.Npgsql -Version 1.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="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrEnum.Npgsql" Version="1.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 1.0.0
                    
#r "nuget: StrEnum.Npgsql, 1.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@1.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=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=StrEnum.Npgsql&version=1.0.0
                    
Install as a Cake Tool

StrEnum.Npgsql

Allows to use StrEnum string enums with Npgsql and Entity Framework Core, including the ability to map them to native Postgres enum types - similar to what MapEnum does for regular C# enums.

Built on top of StrEnum.EntityFrameworkCore.

Supports EF Core 6 – 10.

Installation

You can install StrEnum.Npgsql using the .NET CLI:

dotnet add package StrEnum.Npgsql

Usage

StrEnum.Npgsql lets you choose how Entity Framework stores your string enums in Postgres:

  • as plain text columns (the default — same behaviour as StrEnum.EntityFrameworkCore), or
  • as native Postgres enum types created via CREATE TYPE ... AS ENUM (...).

Storing string enums as text

Define a string enum and an entity that uses it:

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");
}

public class Race
{
    public Guid Id { get; private set; }
    public string Name { get; private set; }
    public Sport Sport { get; private set; }

    private Race() { }

    public Race(string name, Sport sport)
    {
        Id = Guid.NewGuid();
        Name = name;
        Sport = sport;
    }
}

And call UseStringEnums() when configuring your DB context:

public class RaceContext: DbContext
{
    public DbSet<Race> Races { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseNpgsql("Host=localhost;Database=BestRaces;Username=*;Password=*;")
            .UseStringEnums();
    }
}

EF Core will store the Sport property in a text column. Running dotnet ef migrations add Init will produce:

migrationBuilder.CreateTable(
    name: "Races",
    columns: table => new
    {
        Id = table.Column<Guid>(type: "uuid", nullable: false),
        Name = table.Column<string>(type: "text", nullable: false),
        Sport = table.Column<string>(type: "text", nullable: false)
    },
    constraints: table => table.PrimaryKey("PK_Races", x => x.Id));

Storing string enums as Postgres enum types

To map Sport to a Postgres enum type called sport, keep UseStringEnums() on the options builder and call MapStringEnumAsPostgresEnum<Sport>() in OnModelCreating:

public class RaceContext: DbContext
{
    public DbSet<Race> Races { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseNpgsql("Host=localhost;Database=BestRaces;Username=*;Password=*;")
            .UseStringEnums();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Race>();

        modelBuilder.MapStringEnumAsPostgresEnum<Sport>();
    }
}

UseStringEnums() is required in both modes - it teaches EF Core how to recognise StringEnum<T> properties as scalars rather than navigation properties. MapStringEnumAsPostgresEnum<TEnum> then overrides the column type to the Postgres enum.

MapStringEnumAsPostgresEnum<TEnum>() does two things:

  1. Registers a Postgres enum type in the EF model, so a CREATE TYPE migration is produced. Labels are taken from the string enum's underlying values, in declaration order.
  2. Walks all entity types and configures every property of type TEnum to use that Postgres enum as its column type, applying a value converter that maps a Sport member to its underlying string value.

The generated migration will look like this:

migrationBuilder.AlterDatabase()
    .Annotation("Npgsql:Enum:sport", "ROAD_CYCLING,MTB,TRAIL_RUNNING");

migrationBuilder.CreateTable(
    name: "Races",
    columns: table => new
    {
        Id = table.Column<Guid>(type: "uuid", nullable: false),
        Name = table.Column<string>(type: "text", nullable: false),
        Sport = table.Column<Sport>(type: "sport", nullable: false)
    },
    constraints: table => table.PrimaryKey("PK_Races", x => x.Id));
Customizing the Postgres enum name and schema

By default the Postgres enum name is the snake_cased CLR type name (Sportsport). You can override the name and schema:

modelBuilder.MapStringEnumAsPostgresEnum<Sport>(name: "sport_kind", schema: "races");
Configuring individual properties

If you want fine-grained control over which properties map to a Postgres enum, call HasPostgresStringEnum<TEnum>() per property:

modelBuilder.HasPostgresStringEnum<Sport>(); // creates the CREATE TYPE migration

modelBuilder.Entity<Race>()
    .Property(r => r.Sport)
    .HasPostgresStringEnum<Sport>();

HasPostgresStringEnum<TEnum>() on ModelBuilder only registers the type. The PropertyBuilder overload sets the column type and a value converter for that single property.

Mixing both modes

Nothing stops you from using both modes in the same context:

modelBuilder.MapStringEnumAsPostgresEnum<Sport>();    // Sport -> sport enum
// Country has no Postgres-enum mapping, so it stays as text

Properties of Country will be stored as text if you also called .UseStringEnums() on the options builder.

Querying

EF Core translates LINQ operations on string enums into SQL just like in StrEnum.EntityFrameworkCore:

var trailRuns = await context.Races
    .Where(r => r.Sport == Sport.TrailRunning)
    .ToArrayAsync();

When Sport is mapped to a Postgres enum, the parameter is sent and compared as that enum type.

var cyclingSports = new[] { Sport.MountainBiking, Sport.RoadCycling };

var cyclingRaces = await context.Races
    .Where(r => cyclingSports.Contains(r.Sport))
    .ToArrayAsync();

Acknowledgements

Built on top of StrEnum.EntityFrameworkCore and Npgsql.EntityFrameworkCore.PostgreSQL.

License

Copyright © 2026 Dmytro Khmara.

StrEnum is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.

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