StronglyTypedIdentifier.EntityFrameworkCore 0.2.0

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

StronglyTypedIdentifier

Turn an empty partial struct into a real identifier.

public partial struct BlogId : IStronglyTypedIdentifier< Guid >;

That is the whole declaration. Everything below is generated.

var id = new BlogId( Guid.NewGuid() );   // or BlogId.New()

app.MapGet( "/blog/{id}", ( BlogId id ) => ... );   // route binding works
JsonSerializer.Serialize( id );                     // "3f2a…" — a bare string

Why

Guid says nothing. BlogId says everything — and the compiler stops you passing a PostId where a BlogId belongs. The usual objection is the boilerplate: a constructor, equality, ToString, parsing, a JSON converter, a TypeConverter, an EF value converter. Per identifier. Forever.

This generates all of it.

Backing types

Guid, int, long and string.

public partial struct BlogId : IStronglyTypedIdentifier< Guid >;
public partial struct LineNumber : IStronglyTypedIdentifier< int >;
public partial struct Ticks : IStronglyTypedIdentifier< long >;
public partial struct Sku : IStronglyTypedIdentifier< string >;

Numeric identifiers serialize as numbers, not strings. New() is generated only for Guid, since that is the only backing type where the library can invent a value.

What gets generated

Value, constructor the identifier itself
Equals, GetHashCode, ==, != equality on the wrapped value
CompareTo, <, <=, >, >= ordering
ToString, IFormattable, TryFormat formatting
TryParse, Parse, IParsable, ISpanParsable minimal API route and query binding
nested JsonConverter + attribute JSON bodies, and dictionary keys
nested TypeConverter + attribute MVC model binding, IConfiguration.Bind()
implicit unwrap, explicit wrap conversion to and from the backing type

IParsable, ISpanParsable and TryFormat need .NET 7 or later, so they are emitted only under net10.0. Everything else, TryParse included, is emitted for netstandard2.1 too.

Anything you write yourself wins. Declare your own ToString and the generator leaves it alone.

Entity Framework Core

dotnet add package StronglyTypedIdentifier.EntityFrameworkCore
protected override void ConfigureConventions( ModelConfigurationBuilder configurationBuilder )
    => configurationBuilder.UseStronglyTypedIdentifiers();

That covers every identifier in the assembly. Columns hold the raw value, and identifiers are queryable in a translated Where — the conversion is expression-based, not a delegate, so EF can push it into SQL.

This has to run in ConfigureConventions rather than OnModelCreating: EF decides which properties are mappable before the model is built, and a property whose type it does not recognise never becomes a property at all.

ASP.NET Core

dotnet add package StronglyTypedIdentifier.AspNetCore
builder.Services.AddOpenApi( o => o.AddStronglyTypedIdentifiers() );

Without it, the OpenAPI generator meets the generated JSON converter, cannot infer a shape and emits an empty schema — which client generators read as any, losing the type entirely. With it, each identifier is described as the value it wraps:

Backing type Described as
Guid {"type":"string","format":"uuid"}
int {"type":"integer","format":"int32"}
long {"type":"integer","format":"int64"}
string {"type":"string"}

It registers two transformers, because body schemas and route parameters travel different paths through the generator and a schema transformer never sees a parameter.

GUID version

New() produces a version 4 GUID by default. For time-ordered version 7 identifiers, set the property once per project:

<PropertyGroup>
  <StronglyTypedIdentifierGuidVersion>7</StronglyTypedIdentifierGuidVersion>
</PropertyGroup>

Every New() in that project then uses Guid.CreateVersion7(). It needs .NET 9 or later; on an older target the build fails with STI005 rather than silently handing back a version 4 value.

The property only steers New(). The constructor takes any Guid you give it, so new BlogId( Guid.CreateVersion7() ) works with or without the property.

Diagnostics

STI001 the identifier is not partial
STI002 the identifier is not a struct
STI003 the backing type is not Guid, int, long or string
STI004 a containing type is not partial
STI005 version 7 requested, but the target framework predates .NET 9
STI006 StronglyTypedIdentifierGuidVersion is neither 4 nor 7

Notes

A string-backed identifier's constructor rejects null, but default( Sku ).Value is still null — a struct cannot guard its own default. ToString() returns string.Empty there rather than throwing. String identifiers compare ordinally, so they behave the same in every culture.

Product Compatible and additional computed target framework versions.
.NET 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

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
0.2.0 634 9/23/2026