DcsvIo.D2.EntityFrameworkCore.Postgres 0.1.1

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

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 shared across all D2 services. Each service 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 two paths can never drift.

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. The fleet catalog SoT remains contracts/advisory-locks/; DcsvIo.D2.AdvisoryLocks.SourceGen emits into the owning-module assembly.


Install

dotnet add package DcsvIo.D2.EntityFrameworkCore.Postgres

PgAdvisoryLock

Session-scoped PostgreSQL advisory lock helper. Opens a dedicated NpgsqlConnection; each acquired lock scope owns its connection lifecycle.

// 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.


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 surfaces immediately rather than 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 infra/ project:

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>.


NpgsqlContextDefaults.ApplyD2NpgsqlDefaults

Canonical 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 drift.

EnableRetryOnFailure is intentionally absent — see PgAdvisoryLock remarks above.


Configuration

No configuration of its own. Consumers supply:

  • connectionString — the Npgsql connection string (e.g. from MY_DATABASE_URL).
  • commandTimeoutSeconds — per-command timeout (seconds).
  • migrationsAssemblyName — assembly holding the EF Core migrations.
  • migratorLockKey — advisory lock bigint key for the migrator (domain-owned generated AdvisoryLocks.* constant from the owning module assembly).

Dependencies

  • Microsoft.EntityFrameworkCoreDbContext, Database.MigrateAsync, design-time IDesignTimeDbContextFactory.
  • Npgsql.EntityFrameworkCore.PostgreSQLUseNpgsql, NpgsqlDbContextOptionsBuilder.
  • Npgsql — raw NpgsqlConnection for advisory locks and ensure-db maintenance.
  • DcsvIo.D2.TimeAddD2NodaTime() (NodaTime ↔ TIMESTAMPTZ value converters).
  • DcsvIo.D2.UtilitiesThrowIfFalsey / Falsey guards.
  • JetBrains.Annotations[MustDisposeResource] on PgAdvisoryLock.
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.1.1 102 7/17/2026
0.1.0 98 7/17/2026