Dosaic.Plugins.Persistence.EfCore.NpgSql
1.2.30
dotnet add package Dosaic.Plugins.Persistence.EfCore.NpgSql --version 1.2.30
NuGet\Install-Package Dosaic.Plugins.Persistence.EfCore.NpgSql -Version 1.2.30
<PackageReference Include="Dosaic.Plugins.Persistence.EfCore.NpgSql" Version="1.2.30" />
<PackageVersion Include="Dosaic.Plugins.Persistence.EfCore.NpgSql" Version="1.2.30" />
<PackageReference Include="Dosaic.Plugins.Persistence.EfCore.NpgSql" />
paket add Dosaic.Plugins.Persistence.EfCore.NpgSql --version 1.2.30
#r "nuget: Dosaic.Plugins.Persistence.EfCore.NpgSql, 1.2.30"
#:package Dosaic.Plugins.Persistence.EfCore.NpgSql@1.2.30
#addin nuget:?package=Dosaic.Plugins.Persistence.EfCore.NpgSql&version=1.2.30
#tool nuget:?package=Dosaic.Plugins.Persistence.EfCore.NpgSql&version=1.2.30
Dosaic.Plugins.Persistence.EfCore.NpgSql
Dosaic.Plugins.Persistence.EfCore.NpgSql is a Dosaic plugin that integrates PostgreSQL into the EF Core persistence layer via the Npgsql provider. It provides connection and pool configuration, automatic PostgreSQL enum mapping via [DbEnumAttribute], lambda injection support, and a hosted background migration service.
Installation
dotnet add package Dosaic.Plugins.Persistence.EfCore.NpgSql
Or add directly to your .csproj:
<PackageReference Include="Dosaic.Plugins.Persistence.EfCore.NpgSql" Version="" />
Configuration
The plugin binds its configuration from the npgsql section via the [Configuration("npgsql")] attribute on EfCoreNpgSqlConfiguration.
appsettings.yml
npgsql:
host: "localhost"
port: 5432
database: "mydb"
username: "postgres"
password: "postgres"
connectionLifetime: 60 # seconds; 0 = indefinite (default: 60)
keepAlive: 15 # seconds; 0 = disabled (default: 15)
maxPoolSize: 100 # (default: 100)
configureLoggingCacheTimeInSeconds: 300 # (default: 300)
splitQuery: false # use SplitQuery behaviour (default: false)
includeErrorDetail: false # include PG error details — may expose sensitive data
enableDetailedErrors: false # EF Core detailed errors — may expose sensitive data
enableSensitiveDataLogging: false # log parameter values — may expose sensitive data
Configuration class
// Automatically resolved by Dosaic via [Configuration("npgsql")]
[Configuration("npgsql")]
public class EfCoreNpgSqlConfiguration
{
public string Host { get; set; }
public int Port { get; set; } = 5432;
public string Database { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int ConnectionLifetime { get; set; } = 60;
public int KeepAlive { get; set; } = 15;
public int MaxPoolSize { get; set; } = 100;
public int ConfigureLoggingCacheTimeInSeconds { get; set; } = 300;
public bool IncludeErrorDetail { get; set; }
public bool EnableDetailedErrors { get; set; }
public bool EnableSensitiveDataLogging { get; set; }
public bool SplitQuery { get; set; }
}
Usage
Registering a DbContext in a Dosaic plugin
EfCoreNpgSqlConfiguration is injected automatically by the Dosaic TypeImplementationResolver.
Use ConfigureNpgSqlContext<TDbContext> to wire up the Npgsql provider on your DbContextOptionsBuilder.
using Dosaic.Hosting.Abstractions.Plugins;
using Dosaic.Plugins.Persistence.EfCore.Abstractions;
using Dosaic.Plugins.Persistence.EfCore.NpgSql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
public class MyPlugin(EfCoreNpgSqlConfiguration npgsqlConfig)
: IPluginServiceConfiguration, IPluginHealthChecksConfiguration
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(
(provider, options) => options.ConfigureNpgSqlContext<MyDbContext>(provider, npgsqlConfig));
// Optional: run EF migrations in the background on startup
services.AddNpgsqlDbMigratorService<MyDbContext>();
}
public void ConfigureHealthChecks(IHealthChecksBuilder healthChecks)
{
healthChecks.AddEfCoreContext<MyDbContext>();
}
}
Defining a DbContext
using Dosaic.Plugins.Persistence.EfCore.Abstractions.Database;
using Microsoft.EntityFrameworkCore;
public class MyDbContext(DbContextOptions<MyDbContext> options) : EfCoreDbContext(options)
{
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(e =>
{
e.HasKey(x => x.Id);
e.Property(x => x.Id).IsRequired();
});
// register any [DbEnum]-attributed enums for this context
modelBuilder.MapDbEnums<MyDbContext>();
base.OnModelCreating(modelBuilder);
}
}
PostgreSQL enum mapping
Decorate your C# enums with [DbEnum] to have them automatically mapped to PostgreSQL enum types.
PostgresEnumExtensions scans all enums bearing this attribute in the assembly of your TDbContext
(and in the EF Core Abstractions assembly) and registers them with the Npgsql provider.
using Dosaic.Plugins.Persistence.EfCore.Abstractions.Database;
[DbEnum("order_status", "public")]
public enum OrderStatus
{
Pending,
Processing,
Shipped,
Delivered,
Cancelled,
}
The enum values are translated to snake_case names (e.g. Processing → processing) automatically via NpgsqlSnakeCaseNameTranslator.
Advanced: customising the Npgsql options builder
Pass an optional Action<NpgSqlConfiguration> to ConfigureNpgSqlContext to further customise the provider:
services.AddDbContext<MyDbContext>((provider, options) =>
options.ConfigureNpgSqlContext<MyDbContext>(provider, npgsqlConfig, c =>
{
// Provide a pre-built NpgsqlDataSource (e.g. for password rotation)
c.WithDataSource(dataSourceBuilder =>
{
dataSourceBuilder.UsePeriodicPasswordProvider(/* ... */);
});
// Fine-tune the Npgsql EF builder
c.WithNpgSql(npgsql =>
{
npgsql.CommandTimeout(60);
});
// Override EF Core warning behaviour
c.WithWarnings(w =>
{
w.Log((CoreEventId.RowLimitingOperationWithoutOrderByWarning, LogLevel.Debug));
});
// Use a pre-compiled EF Core model (AOT / startup performance)
c.WithModel(MyDbContextModel.Instance);
}));
Background migration service
NpgsqlDbMigratorService<TDbContext> is a hosted BackgroundService that:
- Retrieves all pending EF Core migrations and applies them in order.
- After a successful migration run, reloads the Npgsql type map so that any newly created PostgreSQL enum types are immediately usable.
- Retries on failure with a 1-second delay.
Register it with:
services.AddNpgsqlDbMigratorService<MyDbContext>();
Features
- Npgsql EF Core provider — connects to PostgreSQL using
Npgsql.EntityFrameworkCore.PostgreSQLwith a fully configuredNpgsqlDataSource - Connection pool tuning — configurable
MaxPoolSize,ConnectionLifetime,KeepAlive, andArrayNullabilityMode=PerInstance - Automatic PostgreSQL enum mapping —
[DbEnumAttribute]marks C# enums;MapDbEnums<TDbContext>registers them on both the data source builder and the model builder with snake_case translation - Query splitting — toggle
SplitQueryto switch betweenSingleQueryandSplitQuerybehaviour globally - Lambda injection —
NeinLinqlambda injection is enabled automatically viaWithLambdaInjection() - Configurable logging — logging cache time, detailed errors, and sensitive data logging are all controlled through configuration
- Background migrator —
NpgsqlDbMigratorService<TDbContext>applies pending migrations on startup and reloads Npgsql types afterward - Health check integration — use
AddEfCoreContext<TDbContext>(fromDosaic.Plugins.Persistence.EfCore.Abstractions) to expose a readiness health check for the database context - OpenTelemetry instrumentation —
OpenTelemetry.Instrumentation.EntityFrameworkCoreis included automatically
| 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
- Chronos.Abstractions (>= 2.0.24)
- Dosaic.Hosting.Abstractions (>= 1.2.30)
- Dosaic.Plugins.Persistence.EfCore.Abstractions (>= 1.2.30)
- GracefulExpandoObject (>= 1.6.0)
- Linq.Expression.Optimizer (>= 1.0.35)
- LinqKit.Microsoft.EntityFrameworkCore (>= 10.0.11)
- Microsoft.Build (>= 18.4.0)
- Microsoft.Build.Tasks.Core (>= 18.4.0)
- Microsoft.Build.Utilities.Core (>= 18.4.0)
- Microsoft.CodeAnalysis.Analyzers (>= 5.3.0)
- Microsoft.CodeAnalysis.Common (>= 5.3.0)
- Microsoft.CodeAnalysis.CSharp (>= 5.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 5.3.0)
- Microsoft.CodeAnalysis.Workspaces.Common (>= 5.3.0)
- Microsoft.CodeAnalysis.Workspaces.MSBuild (>= 5.3.0)
- Microsoft.EntityFrameworkCore (>= 10.0.7)
- Microsoft.EntityFrameworkCore.Design (>= 10.0.7)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.7)
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Configuration (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection (>= 10.0.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore (>= 10.0.7)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.7)
- Nanoid (>= 3.1.0)
- NeinLinq.EntityFrameworkCore (>= 7.4.0)
- Newtonsoft.Json (>= 13.0.4)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.1)
- OpenTelemetry (>= 1.15.3)
- OpenTelemetry.Extensions.Hosting (>= 1.15.3)
- OpenTelemetry.Instrumentation.EntityFrameworkCore (>= 1.15.0-beta.1)
- System.Security.Cryptography.Xml (>= 10.0.6)
- Vogen (>= 8.0.5)
- YamlDotNet (>= 17.0.1)
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 |
|---|---|---|
| 1.2.30 | 64 | 5/7/2026 |
| 1.2.29 | 65 | 5/5/2026 |
| 1.2.28 | 73 | 4/30/2026 |
| 1.2.27 | 58 | 4/29/2026 |
| 1.2.26 | 60 | 4/29/2026 |
| 1.2.25 | 69 | 4/27/2026 |
| 1.2.24 | 62 | 4/21/2026 |
| 1.2.23 | 64 | 4/14/2026 |
| 1.2.22 | 61 | 4/10/2026 |
| 1.2.21 | 54 | 4/10/2026 |
| 1.2.20 | 58 | 4/10/2026 |
| 1.2.19 | 63 | 4/9/2026 |
| 1.2.18 | 69 | 4/2/2026 |
| 1.2.17 | 60 | 4/1/2026 |
| 1.2.16 | 56 | 4/1/2026 |
| 1.2.15 | 63 | 3/31/2026 |
| 1.2.14 | 62 | 3/30/2026 |
| 1.2.13 | 61 | 3/26/2026 |
| 1.2.12 | 81 | 3/24/2026 |
| 1.2.11 | 74 | 3/17/2026 |