StrEnum.Dapper 1.0.2

Prefix Reserved
dotnet add package StrEnum.Dapper --version 1.0.2
                    
NuGet\Install-Package StrEnum.Dapper -Version 1.0.2
                    
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.Dapper" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrEnum.Dapper" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="StrEnum.Dapper" />
                    
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.Dapper --version 1.0.2
                    
#r "nuget: StrEnum.Dapper, 1.0.2"
                    
#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.Dapper@1.0.2
                    
#: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.Dapper&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=StrEnum.Dapper&version=1.0.2
                    
Install as a Cake Tool

StrEnum.Dapper

Lets you use StrEnum string enums with Dapper.

Supports Dapper 2.0.4+.

Installation

Install StrEnum.Dapper via the .NET CLI:

dotnet add package StrEnum.Dapper

Usage

Defining a string enum and an entity

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; set; }
    public string Name { get; set; }
    public Sport Sport { get; set; }
}

Initialization

Call StrEnumDapper.UseStringEnums() before the first use of Dapper:

StrEnumDapper.UseStringEnums();

UseStringEnums() searches all loaded assemblies for string enums and registers each with Dapper. Since .NET loads assemblies lazily, make sure the assemblies containing your string enums are loaded before this call — either implicitly, by referencing a type from them, or explicitly:

Assembly.Load(typeof(Sport).Assembly.GetName());

Executing commands

connection.Execute(@"INSERT Races(Id, Name, Sport) values (@id, @name, @sport)",
    new[]
    {
        new { id = Guid.NewGuid(), name = "Chornohora Sky Marathon", sport = Sport.TrailRunning  },
        new { id = Guid.NewGuid(), name = "Cape Town Cycle Tour",    sport = Sport.RoadCycling   },
        new { id = Guid.NewGuid(), name = "Cape Epic",               sport = Sport.MountainBiking }
    });

Running queries

Pass string enums as query parameters and map them onto your entities:

var trailRuns = connection.Query(
    @"SELECT Id, Name, Sport FROM Races WHERE Sport = @sport",
    new { sport = Sport.TrailRunning });

Dapper doesn't yet support string enums in IN clauses, so this won't work:

var cyclingRaces = connection.Query(
    @"SELECT Id, Name, Sport FROM Races WHERE Sport in @sports",
    new { sports = new[] { Sport.TrailRunning, Sport.MountainBiking } });

Cast each member to string as a workaround:

var cyclingRaces = connection.Query(
    @"SELECT Id, Name, Sport FROM Races WHERE Sport in @sports",
    new { sports = new[] { (string)Sport.TrailRunning, (string)Sport.MountainBiking } });

Postgres native enum columns

StrEnum.Dapper flattens StringEnum<T> to its underlying string value at the Dapper layer, which Postgres accepts straight into a text column. Native Postgres enum columns (CREATE TYPE ... AS ENUM) are a different story: Postgres has no implicit cast from text to a user-defined enum, so you'll see:

42804: column "sport" is of type sport but expression is of type text

StrEnum.Dapper does not currently bind the parameter to the enum's OID for you. Two practical workarounds:

  • Cast in SQL: add ::your_enum to the parameter site. Works against existing tables without ceremony.

    connection.Execute(
        "INSERT INTO races (id, name, sport) VALUES (@id, @name, @sport::sport)",
        new { id = Guid.NewGuid(), name = "Chornohora Sky Marathon", sport = Sport.TrailRunning });
    
  • Bypass the Dapper handler for that parameter with a hand-rolled NpgsqlParameter that sets DataTypeName:

    var p = new DynamicParameters();
    p.Add("id", Guid.NewGuid());
    p.Add("name", "Chornohora Sky Marathon");
    p.Add(new NpgsqlParameter
    {
        ParameterName = "sport",
        DataTypeName = "sport",
        Value = (string)Sport.TrailRunning,
    });
    connection.Execute("INSERT INTO races (id, name, sport) VALUES (@id, @name, @sport)", p);
    

If you only need raw Npgsql wire support (no Dapper), use StrEnum.NpgsqlNpgsqlDataSourceBuilder.MapStringEnum<T>() binds Sport instances directly to the enum OID. There's no first-class StrEnum.Npgsql.Dapper package today; if you'd find one useful, please open an issue.

License

Copyright © 2026 Dmytro Khmara.

StrEnum is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 145 4/27/2026
1.0.1 1,364 8/1/2022
1.0.0 375 8/1/2022