DcsvIo.D2.EntityFrameworkCore.Postgres
0.4.13
dotnet add package DcsvIo.D2.EntityFrameworkCore.Postgres --version 0.4.13
NuGet\Install-Package DcsvIo.D2.EntityFrameworkCore.Postgres -Version 0.4.13
<PackageReference Include="DcsvIo.D2.EntityFrameworkCore.Postgres" Version="0.4.13" />
<PackageVersion Include="DcsvIo.D2.EntityFrameworkCore.Postgres" Version="0.4.13" />
<PackageReference Include="DcsvIo.D2.EntityFrameworkCore.Postgres" />
paket add DcsvIo.D2.EntityFrameworkCore.Postgres --version 0.4.13
#r "nuget: DcsvIo.D2.EntityFrameworkCore.Postgres, 0.4.13"
#:package DcsvIo.D2.EntityFrameworkCore.Postgres@0.4.13
#addin nuget:?package=DcsvIo.D2.EntityFrameworkCore.Postgres&version=0.4.13
#tool nuget:?package=DcsvIo.D2.EntityFrameworkCore.Postgres&version=0.4.13
DcsvIo.D2.EntityFrameworkCore.Postgres
Audience: backend .NET service engineers wiring a PostgreSQL-backed EF Core DbContext with advisory-lock-guarded migrations and startup validation.
PostgreSQL-specific EF Core startup machinery for .NET hosts that use Npgsql.
Each host that owns a per-domain database registers
AdvisoryLockMigrator<TContext> to safely bootstrap migrations across multiple
replicas, and calls ApplyD2NpgsqlDefaults from both the DI registration and the
design-time factory so the runtime and design-time paths cannot differ.
Mechanism only. Domain advisory-lock key catalogs do not ship from this
package. Callers pass domain-owned generated constants (e.g.
AdvisoryLocks.SampleDomain.MIGRATOR from the owning host/module assembly) into
PgAdvisoryLock / AdvisoryLockMigrator. Catalogs are host-supplied
(advisory-locks.spec.json AdditionalFiles); DcsvIo.D2.AdvisoryLocks.SourceGen
emits into the owning-module assembly via MSBuild emit props.
Install
dotnet add package DcsvIo.D2.EntityFrameworkCore.Postgres
PgAdvisoryLock
Session-scoped PostgreSQL advisory lock helper. Opens a dedicated
NpgsqlConnection; each acquired lock scope manages the lifecycle of its own connection.
// Try-acquire (non-blocking - skip if held):
// Pass a domain-owned generated constant from the owning host/module assembly.
await using var rotLock = await PgAdvisoryLock.TryAcquireSessionAsync(
connStr, migratorLockKey /* e.g. AdvisoryLocks.MyService.ROTATION */, ct);
if (!rotLock.IsHeld)
return; // another instance is rotating - skip this tick
// Blocking acquire (migrator):
await using var migLock = await PgAdvisoryLock.AcquireSessionBlockingAsync(
connStr, migratorLockKey /* e.g. AdvisoryLocks.MyService.MIGRATOR */, ct);
// migLock.IsHeld is always true after this returns
PgAdvisoryLock is [MustDisposeResource] - always use await using. DisposeAsync
sends an explicit pg_advisory_unlock and closes the connection.
No EnableRetryOnFailure: an execution-strategy reconnect silently drops a session
lock. Services using advisory locks must handle transient failures at the application
level.
PgClassedAdvisoryLock and PgRowLock
Both types ship in DcsvIo.D2.EntityFrameworkCore, the provider-neutral package, and are
documented in its README. Their statements are PostgreSQL; neither needs a driver type, and
the application-layer callers that take these locks may not reference the Npgsql dependencies of
this package. Their namespace is DcsvIo.D2.EntityFrameworkCore, and a project references that
package to use them.
AdvisoryLockMigrator<TContext>
IHostedService that runs at host startup: ensure-database → blocking lock → migrate
→ release.
Register it before any hosted services that require the schema:
services.AddSingleton<AdvisoryLockMigrator<MyDbContext>>(sp =>
new AdvisoryLockMigrator<MyDbContext>(
sp.GetRequiredService<IServiceScopeFactory>(),
connectionString,
migratorLockKey, // domain-owned generated AdvisoryLocks.{Db}.MIGRATOR
sp.GetRequiredService<ILogger<AdvisoryLockMigrator<MyDbContext>>>()));
services.AddHostedService<AdvisoryLockMigrator<MyDbContext>>();
The migrator is fail-fast: a bad migration throws, crash-looping the host so the problem shows immediately instead of the host starting with a corrupt schema.
DesignTimeDbContextFactoryBase<TContext>
Abstract base for EF Core design-time factories in module-within-host services (no
Sdk.Web startup project for dotnet ef). Subclass in the project that owns the context and its migrations:
public sealed class MyDbContextFactory
: DesignTimeDbContextFactoryBase<MyDbContext>
{
protected override string ConnectionStringEnvVar => "MY_DATABASE_URL";
protected override string MigrationsAssemblyName =>
typeof(MyDbContextFactory).Assembly.GetName().Name!;
protected override MyDbContext CreateContext(DbContextOptions<MyDbContext> opts)
=> new(opts);
}
Set MY_DATABASE_URL before running dotnet ef migrations add <Name>.
dotnet ef calls CreateDbContext(args), which reads the variable that ConnectionStringEnvVar
specifies, throws InvalidOperationException when it is unset or blank, and passes the connection
string, CommandTimeoutSeconds and MigrationsAssemblyName to ApplyD2NpgsqlDefaults before
handing the options to CreateContext. Override CommandTimeoutSeconds to change the command
timeout from its default of 30 seconds.
NpgsqlContextDefaults.ApplyD2NpgsqlDefaults
The DbContextOptionsBuilder extension that applies UseNpgsql with
AddD2NodaTime(), CommandTimeout, and MigrationsAssembly. Call from BOTH the
runtime DI lambda AND the design-time factory so neither path can differ from the other.
EnableRetryOnFailure is absent on purpose - see PgAdvisoryLock.
NodaTimeValueConverterExtensions.AddD2NodaTime
NpgsqlDbContextOptionsBuilder extension that turns on the Npgsql NodaTime
plugin, which maps Instant to timestamptz, LocalDateTime to timestamp,
LocalDate to date and the rest of the suite. Call it inside the UseNpgsql
configuration lambda; ApplyD2NpgsqlDefaults already does.
services.AddDbContext<MyDbContext>(opts =>
opts.UseNpgsql(connectionString, npgsql => npgsql.AddD2NodaTime()));
Idempotent - calling it twice on the same builder is safe. It lives here rather than beside the NodaTime types themselves because the plugin it turns on is PostgreSQL's, and a consumer of those types on another provider has no use for it.
Configuration
No configuration of its own. Consumers supply:
connectionString- the Npgsql connection string (e.g. fromMY_DATABASE_URL).commandTimeoutSeconds- per-command timeout (seconds).migrationsAssemblyName- assembly holding the EF Core migrations.migratorLockKey- advisory lock bigint key for the migrator (domain-owned generatedAdvisoryLocks.*constant from the owning module assembly).
Telemetry
AdvisoryLockMigrator<TContext> uses [LoggerMessage] (no Exception params):
| EventId | Name | Level | Message shape |
|---|---|---|---|
| 1001 | Starting |
Information | AdvisoryLockMigrator starting for context {ContextName}. |
| 1002 | LockAcquired |
Debug | Migration advisory lock {LockKey} acquired. |
| 1003 | NoPendingMigrations |
Information | No pending migrations for {ContextName}. |
| 1004 | ApplyingMigrations |
Information | Applying pending migrations for {ContextName}. |
| 1005 | MigrationsApplied |
Information | Migrations applied for {ContextName}. |
| 1006 | CreatingDatabase |
Information | Creating database '{DatabaseName}'. |
| 1007 | DatabaseCreated |
Information | Database '{DatabaseName}' created. |
| 1008 | DatabaseAlreadyExists |
Debug | Database '{DatabaseName}' already exists. |
PgAdvisoryLock and ApplyD2NpgsqlDefaults emit no package-owned telemetry.
Dependencies
Microsoft.EntityFrameworkCore-DbContext,Database.MigrateAsync, design-timeIDesignTimeDbContextFactory.Npgsql.EntityFrameworkCore.PostgreSQL-UseNpgsql,NpgsqlDbContextOptionsBuilder.Npgsql- rawNpgsqlConnectionfor advisory locks and ensure-db maintenance.Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime- theUseNodaTime()pluginAddD2NodaTime()calls.DcsvIo.D2.Utilities-ThrowIfFalsey/Falseyguards.JetBrains.Annotations-[MustDisposeResource]onPgAdvisoryLock.
| Product | Versions 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. |
-
net10.0
- DcsvIo.D2.Utilities (= 0.3.10)
- JetBrains.Annotations (>= 2025.2.4)
- Microsoft.EntityFrameworkCore (>= 10.0.7)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.7)
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Caching.Memory (>= 10.0.7)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Logging (>= 10.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Options (>= 10.0.7)
- NodaTime (>= 3.2.2)
- Npgsql (>= 10.0.2)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.1)
- Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime (>= 10.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DcsvIo.D2.EntityFrameworkCore.Postgres:
| Package | Downloads |
|---|---|
|
DcsvIo.D2.Outbox.EntityFrameworkCore.Postgres
PostgreSQL relay for the D2 transactional outbox. |
GitHub repositories
This package is not used by any popular GitHub repositories.