Lyo.Postgres
2.0.0
dotnet add package Lyo.Postgres --version 2.0.0
NuGet\Install-Package Lyo.Postgres -Version 2.0.0
<PackageReference Include="Lyo.Postgres" Version="2.0.0" />
<PackageVersion Include="Lyo.Postgres" Version="2.0.0" />
<PackageReference Include="Lyo.Postgres" />
paket add Lyo.Postgres --version 2.0.0
#r "nuget: Lyo.Postgres, 2.0.0"
#:package Lyo.Postgres@2.0.0
#addin nuget:?package=Lyo.Postgres&version=2.0.0
#tool nuget:?package=Lyo.Postgres&version=2.0.0
Lyo.Postgres
Shared PostgreSQL host bits for Lyo libraries that own an EF Core schema (Audit, Email, ChangeTracker, EntityReference, etc.). When the consumer options opt in, a thin IHostedService calls DbContext.Database.MigrateAsync at host startup. The rest of the package supplies the pieces every Lyo.*.Postgres package used to re-implement: schema-scoped DbContextOptions building, IDbContextFactory<TContext> registration, design-time option construction, and a standard health probe.
Out of scope: entity models and mappings. Each consumer package owns its own
DbContext, entities, and migrations; this package only supplies the plumbing around them.
Features
- Hosted migrations.
AddPostgresMigrations<TContext, TOptions>()plusPostgresMigrationHostedService<TContext, TOptions>, driven from theIPostgresMigrationConfigoptions contract. PostgresOptionsBase. Base options class that implementsIPostgresMigrationConfigwithConnectionString,EnableAutoMigrations, and aValidate()hook. Derived options supply the schema through the abstractSchemaNameproperty, so a package declares its schema once instead of repeating it on every path that builds a connection.PostgresSchema.BuildOptions<TContext>(connectionString, schema)andCreateContext<TContext>produce a context whose migrations history lives at"<schema>"."__EFMigrationsHistory".EnsureAsyncissuesCREATE SCHEMA IF NOT EXISTSwith the schema name escaped.PostgresDesignTime.CreateOptions<TContext>. BuildsDbContextOptions<TContext>fordotnet effrom an environment variable plus an optional fallback connection string, so each package'sIDesignTimeDbContextFactorystays a one-liner.PostgresHealth.CheckAsync. Yields aLyo.HealthHealthResultfrom either anIDbContextFactory<TContext>or a liveDbContext, and reports the schema under thedatabasemetadata key.AddPostgresDbContextFactory<TContext, TOptions>(options). Registers the options instance plus a pooled-styleIDbContextFactory<TContext>bound to that options' connection string and schema.
Examples
Usage
using Lyo.Postgres;
using Microsoft.Extensions.DependencyInjection;
public sealed class AuditDbOptions : IPostgresMigrationConfig
{
public string ConnectionString { get; init; } = "";
public bool EnableAutoMigrations { get; init; }
public string Schema { get; init; } = "audit";
}
services.Configure<AuditDbOptions>(configuration.GetSection("Audit:Postgres"));
services.AddPostgresMigrations<AuditDbContext, AuditDbOptions>();
Types
| Type | Role |
|---|---|
IPostgresMigrationConfig |
Options contract for hosted migrations. Members are string ConnectionString, bool EnableAutoMigrations, string Schema. |
PostgresMigrationHostedService<TContext, TOptions> |
IHostedService that, on StartAsync, scopes a fresh TContext over the configured connection string, runs CREATE SCHEMA IF NOT EXISTS "<Schema>", then calls MigrateAsync. |
Extensions.AddPostgresMigrations<TContext, TOptions>() |
Registers the hosted service. Both type parameters are constrained (TContext : DbContext, TOptions : class, IPostgresMigrationConfig). |
Extensions.AddPostgresDbContextFactory<TContext, TOptions>(options) |
Registers options as a singleton and an IDbContextFactory<TContext> over PostgresSchema.BuildOptions. Every Add{Feature}Postgres shares this registration. |
PostgresOptionsBase |
Abstract IPostgresMigrationConfig implementation. Derived options declare their schema once via the protected abstract SchemaName, and override Validate() for package-specific checks. |
PostgresSchema |
MigrationsHistoryTable constant, BuildOptions<TContext> / CreateContext<TContext> (schema-scoped UseNpgsql), plus EnsureAsync (CREATE SCHEMA IF NOT EXISTS). |
PostgresDesignTime.CreateOptions<TContext> |
Design-time DbContextOptions<TContext> from an environment variable, plus an optional fallback connection string for local dotnet ef runs. |
PostgresHealth.CheckAsync |
HealthResult probe over an IDbContextFactory<TContext> or a live DbContext. Reports the schema under the database metadata key. |
The hosted service activates a TContext instance via Activator.CreateInstance(typeof(TContext), dbContextOptions), so each consumer DbContext must expose a public
constructor that takes a single DbContextOptions<TContext>. The migrations history table is stored in the schema returned by IPostgresMigrationConfig.Schema as
"<schema>"."__EFMigrationsHistory".
StartAsync short-circuits when EnableAutoMigrations is false. If the flag is true, both ConnectionString and Schema are required (whitespace throws via
Lyo.Exceptions.ArgumentHelpers).
Usage
- The hosted service resolves
IOptions<AuditDbOptions>. - When
EnableAutoMigrationsis false, it returns immediately. - Otherwise it builds an
AuditDbContextwithUseNpgsql(connectionString, opt => opt.MigrationsHistoryTable("__EFMigrationsHistory", schema)). - It runs
CREATE SCHEMA IF NOT EXISTS "<schema>"(the schema name is escaped by doubling embedded"characters before interpolation). - Then it runs
Database.MigrateAsync(ct).
Consumers
Lyo.Audit.PostgresLyo.Email.PostgresLyo.ChangeTracker.PostgresLyo.EntityReference.PostgresLyo.FileMetadataStore.Postgres
Dependencies
Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).
Lyo.Exceptions(direct, lyo)Lyo.Health(direct, lyo)Microsoft.EntityFrameworkCore10.0.5(direct, microsoft)Microsoft.EntityFrameworkCore.Design10.0.5(direct, microsoft)Microsoft.EntityFrameworkCore.Relational10.0.5(direct, microsoft)Microsoft.Extensions.Hosting.Abstractions10.0.5(direct, microsoft)Microsoft.Extensions.Options10.0.5(direct, microsoft)Npgsql.EntityFrameworkCore.PostgreSQL10.0.3(direct, third-party)
| 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
- Lyo.Exceptions (>= 2.0.0)
- Lyo.Health (>= 2.0.0)
- Microsoft.EntityFrameworkCore (>= 10.0.5)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.5)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 10.0.3)
NuGet packages (28)
Showing the top 5 NuGet packages that depend on Lyo.Postgres:
| Package | Downloads |
|---|---|
|
Lyo.EntityReference.Postgres
Entity Framework Core base entity, configuration, and store helpers for canonical entity-ref rows on PostgreSQL. |
|
|
Lyo.Reporting.Postgres
PostgreSQL reporting data layer with EF Core, optional auto-migrations, and report generation orchestration. |
|
|
Lyo.Job.Postgres
PostgreSQL persistence for Lyo job management with EF Core, optional auto-migrations, and RabbitMQ event publishing. |
|
|
Lyo.FileMetadataStore.Postgres
PostgreSQL implementation of the Lyo FileMetadataStore service using Entity Framework Core. |
|
|
Lyo.Sms.Twilio.Postgres
PostgreSQL implementation of Lyo.Sms.Twilio using Entity Framework Core. |
GitHub repositories
This package is not used by any popular GitHub repositories.