StrEnum.EntityFrameworkCore
2.4.2
Prefix Reserved
dotnet add package StrEnum.EntityFrameworkCore --version 2.4.2
NuGet\Install-Package StrEnum.EntityFrameworkCore -Version 2.4.2
<PackageReference Include="StrEnum.EntityFrameworkCore" Version="2.4.2" />
<PackageVersion Include="StrEnum.EntityFrameworkCore" Version="2.4.2" />
<PackageReference Include="StrEnum.EntityFrameworkCore" />
paket add StrEnum.EntityFrameworkCore --version 2.4.2
#r "nuget: StrEnum.EntityFrameworkCore, 2.4.2"
#:package StrEnum.EntityFrameworkCore@2.4.2
#addin nuget:?package=StrEnum.EntityFrameworkCore&version=2.4.2
#tool nuget:?package=StrEnum.EntityFrameworkCore&version=2.4.2
StrEnum.EntityFrameworkCore
Lets you use StrEnum string enums with Entity Framework Core.
Supports EF Core 3.1 – 10.
Installation
Install StrEnum.EntityFrameworkCore via the .NET CLI:
dotnet add package StrEnum.EntityFrameworkCore
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; 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;
}
}
Wiring it up
Call UseStringEnums() when configuring your DB context:
public class RaceContext: DbContext
{
public DbSet<Race> Races { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=.;Database=BestRaces;user id=*;pwd=*;")
.UseStringEnums();
}
}
That's it — EF Core can now read and write string enums.
Migrations
EF Core stores string enums in non-nullable string columns (NVARCHAR(MAX) in SQL Server, TEXT in Postgres).
If you'd like to store string enums as native Postgres enum types instead of
TEXT, see StrEnum.Npgsql.EntityFrameworkCore.
Running dotnet ef migrations add Init produces:
migrationBuilder.CreateTable(
name: "Races",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Sport = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Races", x => x.Id);
});
To allow a nullable string enum, mark the property as non-required when configuring the entity:
var race = builder.Entity<Race>();
race.Property(p => p.Sport).IsRequired(false);
Querying
EF Core translates LINQ operations on string enums into SQL.
Add some races first:
var context = new RaceContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
var races = new[]
{
new Race("Chornohora Sky Marathon", Sport.TrailRunning),
new Race("Cape Town Cycle Tour", Sport.RoadCycling),
new Race("Cape Epic", Sport.MountainBiking)
};
await context.Races.AddRangeAsync(races);
await context.SaveChangesAsync();
Filter by a single Sport:
var trailRuns = await context.Races.Where(r => r.Sport == Sport.TrailRunning).ToArrayAsync();
This produces:
SELECT [r].[Id], [r].[Name], [r].[Sport]
FROM [Races] AS [r]
WHERE [r].[Sport] = N'TRAIL_RUNNING'
Or by multiple Sport values:
var cyclingSports = new[] { Sport.MountainBiking, Sport.RoadCycling };
var cyclingRaces = await context.Races.Where(r => cyclingSports.Contains(r.Sport)).ToArrayAsync();
Which translates to:
SELECT [r].[Id], [r].[Name], [r].[Sport]
FROM [Races] AS [r]
WHERE [r].[Sport] IN (N'MTB', N'ROAD_CYCLING')
Acknowledgements
Thanks to Andrew Lock for his research on using a custom ValueConverterSelector.
License
Copyright © 2026 Dmytro Khmara.
StrEnum is licensed under the MIT license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. 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. |
| .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 is compatible. |
| .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. |
-
.NETStandard 2.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 5.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
.NETStandard 2.1
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 6.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
net10.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 11.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
net6.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 7.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
net7.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 8.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
net8.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 9.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
-
net9.0
- Microsoft.EntityFrameworkCore (>= 3.1.0 && < 10.0.0)
- StrEnum (>= 2.0.0 && < 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.