JorgeCostaMacia.ValueObject.EfConverter
6.0.4
Prefix Reserved
dotnet add package JorgeCostaMacia.ValueObject.EfConverter --version 6.0.4
NuGet\Install-Package JorgeCostaMacia.ValueObject.EfConverter -Version 6.0.4
<PackageReference Include="JorgeCostaMacia.ValueObject.EfConverter" Version="6.0.4" />
<PackageVersion Include="JorgeCostaMacia.ValueObject.EfConverter" Version="6.0.4" />
<PackageReference Include="JorgeCostaMacia.ValueObject.EfConverter" />
paket add JorgeCostaMacia.ValueObject.EfConverter --version 6.0.4
#r "nuget: JorgeCostaMacia.ValueObject.EfConverter, 6.0.4"
#:package JorgeCostaMacia.ValueObject.EfConverter@6.0.4
#addin nuget:?package=JorgeCostaMacia.ValueObject.EfConverter&version=6.0.4
#tool nuget:?package=JorgeCostaMacia.ValueObject.EfConverter&version=6.0.4
JorgeCostaMacia.ValueObject.EfConverter
Native EF Core value converters for JorgeCostaMacia.ValueObject value objects — one converter per family (value object <-> underlying value), applied explicitly per property. Provider-agnostic (works with Npgsql, SQL Server, SQLite, …).
Install
dotnet add package JorgeCostaMacia.ValueObject.EfConverter
Brings JorgeCostaMacia.ValueObject and Microsoft.EntityFrameworkCore (the provider-agnostic core — not a database provider). Your own provider (e.g. Npgsql.EntityFrameworkCore.PostgreSQL) builds on the same core, so there is no conflict; NuGet resolves EF Core to your provider's version.
Usage
Apply a converter
using JorgeCostaMacia.ValueObject.EfConverter.Infrastructure;
builder.Property(e => e.Email).HasConversion(new StringValueObjectConverter<Email>());
One converter per family — IntValueObjectConverter<>, StringValueObjectConverter<>, DecimalValueObjectConverter<>, … — closed over your concrete value object. Map each property explicitly: it keeps the mapped surface intentional (a transient value object never gets persisted by accident) and the column type under your control. Multi-field value objects (ranges) are not single-column and are not covered here.
The same converter serves required and nullable properties (Email and Email?): EF maps a null reference to a NULL column and only invokes the converter for non-null values. For a nullable property, pass the converter type — .HasConversion(typeof(StringValueObjectConverter<Email>)) — to avoid a nullable-reference warning (CS8620) on the instance overload.
Value object as a primary key
A value object works as a key via its converter (complex/owned types cannot be keys). Configure it explicitly, since a converted key is app-assigned:
builder.HasKey(e => e.Id);
builder.Property(e => e.Id)
.HasConversion(new IntValueObjectConverter<UserId>())
.ValueGeneratedNever();
Notes
- Rehydration goes through the value object's constructor (found by reflection, compiled once) — not the
Createfactory — so reads do not re-run domain validation on already-persisted data. The constructor may be non-public. - Querying by the underlying value uses the value object's conversion operator, e.g.
where (int)e.Id > 1000orwhere ((string)e.Name).StartsWith("a"). byte[]-backed value objects (ByteValueObject):byte[]is a mutable reference type, so EF Core needs aValueComparerto detect changes by content rather than by reference. Value objects are immutable (you replace the whole object, which EF sees as a new reference), so this only matters if you mutate the array in place — an anti-pattern. If you do, configure a comparer on that property.
About
Part of shared-net.
Author: Jorge Costa Maciá — GitHub
| Product | Versions 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. |
-
net10.0
- JorgeCostaMacia.ValueObject (>= 6.0.4)
- Microsoft.EntityFrameworkCore (>= 10.0.10)
-
net8.0
- JorgeCostaMacia.ValueObject (>= 6.0.4)
- Microsoft.EntityFrameworkCore (>= 9.0.18)
-
net9.0
- JorgeCostaMacia.ValueObject (>= 6.0.4)
- Microsoft.EntityFrameworkCore (>= 9.0.18)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.