Plex.Extensions.DbContext
8.0.24
dotnet add package Plex.Extensions.DbContext --version 8.0.24
NuGet\Install-Package Plex.Extensions.DbContext -Version 8.0.24
<PackageReference Include="Plex.Extensions.DbContext" Version="8.0.24" />
<PackageVersion Include="Plex.Extensions.DbContext" Version="8.0.24" />
<PackageReference Include="Plex.Extensions.DbContext" />
paket add Plex.Extensions.DbContext --version 8.0.24
#r "nuget: Plex.Extensions.DbContext, 8.0.24"
#:package Plex.Extensions.DbContext@8.0.24
#addin nuget:?package=Plex.Extensions.DbContext&version=8.0.24
#tool nuget:?package=Plex.Extensions.DbContext&version=8.0.24
Plex.Extensions.DbContext
A .NET 8 library that simplifies EF Core DbContext registration with Microsoft Dependency Injection. It supports SQL Server and PostgreSQL, dynamic per-request connection strings (multi-tenant), optional EF Core interceptors (caching), and raw SQL connection factories for Dapper.
Features
- DbContext registration — pooled (
AddDbContextPool) or standard (AddDbContext) with a single method call - Multi-provider — SQL Server and PostgreSQL, switchable per database via configuration
- Dynamic connection strings — resolve different databases per HTTP request using
cx-dbandcx-serverheaders (multi-tenant) - EF Core options from config — command timeout, retry-on-failure, lazy loading, change tracking, query tracking, and migrations all driven by
appsettings.json - Cache interceptor support — optionally wire a
DbCommandInterceptorfor query caching - Raw SQL connection factory — register
IDbConnectionimplementations (e.g., for Dapper) with the same dynamic connection string logic - Assembly scanning — auto-register all implementations of an interface from specified assemblies
Installation
dotnet add package Plex.Extensions.DbContext
Configuration
Add these settings to your appsettings.json (all are optional — defaults shown):
{
"ConnectionStringKey": "DefaultConnection",
"ConnectionStrings": {
"DefaultConnection": "Server=%server%;Database=%db%;Trusted_Connection=True;"
},
"EfSqlCommandTimeOutInSecond": 300,
"EfSqlMaxRetryOnFailureCount": 0,
"EfEnableMigration": false,
"EfUseLazyLoading": false,
"EfUseChangeTrackingProxies": false,
"EfUseQueryTrackingBehavior": false,
"AppSettings": {
"DbProviderMappings": {
"my-database-name": "mssql",
"my-pg-database": "postgresql"
}
}
}
| Setting | Default | Description |
|---|---|---|
ConnectionStringKey |
"ConnectionStringKey" |
The key in ConnectionStrings section to use |
EfSqlCommandTimeOutInSecond |
300 |
SQL command timeout in seconds |
EfSqlMaxRetryOnFailureCount |
0 |
Max automatic retries on transient failures (0 = disabled) |
EfEnableMigration |
false |
Set MigrationsAssembly to the DbContext's assembly |
EfUseLazyLoading |
false |
Enable EF Core lazy loading proxies |
EfUseChangeTrackingProxies |
false |
Enable EF Core change tracking proxies |
EfUseQueryTrackingBehavior |
false |
If false, sets QueryTrackingBehavior.NoTracking |
AppSettings:DbProviderMappings |
null |
Maps database names to providers ("mssql" or "postgresql") |
Usage
Register a single DbContext (pooled)
// In Program.cs or Startup.cs
builder.Services.RegisterDbContextPool<MyDbContext>();
This registers MyDbContext as a pooled DbContext with all EF options resolved from configuration.
Register a single DbContext (standard)
builder.Services.RegisterDbContext<MyDbContext>();
Register with a cache interceptor
// Pooled
builder.Services.RegisterDbContextPool<MyDbContext, MyCacheInterceptor>();
// Standard
builder.Services.RegisterDbContext<MyDbContext, MyCacheInterceptor>();
The interceptor (DbCommandInterceptor) is resolved from DI and added to the DbContext options if registered.
Register multiple DbContexts
builder.Services.RegisterDbContextsPool(services =>
{
var key = services.RegisterDbContextPool<FirstDbContext>();
key = services.RegisterDbContextPool<SecondDbContext>(key);
return key;
});
Register a raw SQL connection factory (for Dapper)
builder.Services.RegisterSqlConnectionFactory<ISqlConnectionFactory, SqlConnectionFactory>();
This creates a connection with the dynamically resolved connection string, automatically appending Command Timeout and TrustServerCertificate=True if not already present.
Assembly scanning
builder.Services.RegisterFromAssemblyTypes<IMyService>(
typeof(MyServiceImpl).Assembly
);
Scans the given assemblies for all types implementing IMyService and registers them as scoped services.
Dynamic Connection Strings (Multi-Tenant)
The library supports per-request database targeting via HTTP headers:
| Header | Purpose |
|---|---|
cx-db |
Overrides the database name in the connection string |
cx-server |
Overrides the server in the connection string |
Connection string templates can use %db% and %server% placeholders that get replaced at runtime. Resolved connection strings are cached in memory for performance.
API Reference
ServiceCollectionExtensions
| Method | Description |
|---|---|
RegisterDbContextPool<TContext>(key?) |
Register a pooled DbContext |
RegisterDbContextPool<TContext, TCacheInterceptor>(key?) |
Register a pooled DbContext with cache interceptor |
RegisterDbContextsPool(Func<IServiceCollection, string>) |
Register multiple pooled DbContexts |
RegisterDbContext<TContext>(key?) |
Register a standard DbContext |
RegisterDbContext<TContext, TCacheInterceptor>(key?) |
Register a standard DbContext with cache interceptor |
RegisterDbContexts(Func<IServiceCollection, string>) |
Register multiple standard DbContexts |
RegisterSqlConnectionFactory<IConn, TConn>(key?) |
Register a raw SQL connection factory |
RegisterFromAssemblyTypes<IService>(assemblies) |
Scan and register all implementations of an interface |
ResolveServiceWithKey<IService>(key?) |
Resolve a keyed service from IEnumerable<IService> |
ConfigurationExtensions
| Method | Description |
|---|---|
GetDynamicConnectionString(httpRequest?, logger?, dbProviderMappings?, connectionStringKey) |
Resolve a connection string with dynamic header-based overrides |
PlexDbOptions
Configuration object registered in DI that holds all EF Core settings and the resolved connection string for the current request.
Dependencies
| Package | Version |
|---|---|
| Microsoft.EntityFrameworkCore | 9.0.x |
| Microsoft.EntityFrameworkCore.SqlServer | 9.0.x |
| Npgsql.EntityFrameworkCore.PostgreSQL | 9.0.x |
| Microsoft.EntityFrameworkCore.Proxies | 9.0.x |
| Microsoft.Extensions.DependencyInjection | 10.0.x |
| Plex.Extensions.Configuration | 8.0.x |
| HotChocolate.Data.EntityFramework | 16.5.x |
License
Plex-Solution Community Source-Available License — free for non-commercial use only.
| 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 was computed. 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 was computed. 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. |
-
net8.0
- HotChocolate.Data.EntityFramework (>= 16.5.1)
- Microsoft.EntityFrameworkCore (>= 9.0.18)
- Microsoft.EntityFrameworkCore.Proxies (>= 9.0.18)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.18)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.18)
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
- Microsoft.Identity.Client (>= 4.86.1)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.4)
- Plex.Extensions.Configuration (>= 8.0.7)
- System.Formats.Asn1 (>= 10.0.10)
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 |
|---|---|---|
| 8.0.24 | 36 | 7/23/2026 |
| 8.0.23 | 38 | 7/21/2026 |
| 8.0.21 | 234 | 12/8/2024 |
| 8.0.20 | 357 | 10/24/2024 |
| 8.0.19 | 443 | 9/9/2024 |
| 8.0.18 | 343 | 8/19/2024 |
| 8.0.17 | 260 | 7/20/2024 |
| 8.0.16 | 360 | 7/15/2024 |
| 8.0.15 | 291 | 7/2/2024 |
| 8.0.14 | 234 | 6/28/2024 |
| 8.0.12 | 241 | 6/25/2024 |
| 8.0.11 | 212 | 6/25/2024 |
| 8.0.10 | 209 | 6/25/2024 |
| 8.0.9 | 222 | 6/25/2024 |
| 8.0.8 | 198 | 6/24/2024 |
| 8.0.7 | 208 | 6/24/2024 |
| 8.0.6 | 233 | 6/13/2024 |
| 8.0.3 | 202 | 6/29/2024 |
Upgrade "HotChocolate.Data.EntityFramework" to version "16.5.1"