MagicCSharp.Data.Postgres
1.0.2
dotnet add package MagicCSharp.Data.Postgres --version 1.0.2
NuGet\Install-Package MagicCSharp.Data.Postgres -Version 1.0.2
<PackageReference Include="MagicCSharp.Data.Postgres" Version="1.0.2" />
<PackageVersion Include="MagicCSharp.Data.Postgres" Version="1.0.2" />
<PackageReference Include="MagicCSharp.Data.Postgres" />
paket add MagicCSharp.Data.Postgres --version 1.0.2
#r "nuget: MagicCSharp.Data.Postgres, 1.0.2"
#:package MagicCSharp.Data.Postgres@1.0.2
#addin nuget:?package=MagicCSharp.Data.Postgres&version=1.0.2
#tool nuget:?package=MagicCSharp.Data.Postgres&version=1.0.2
MagicCSharp.Data.Postgres
PostgreSQL wiring for MagicCSharp.Data.EntityFramework: a pooled context factory read from configuration
that fails at startup rather than on the first query, a design-time factory for dotnet ef, and an
interceptor for the one timestamptz mistake that only ever shows up on a developer's machine.
Add it to the data project alongside MagicCSharp.Data.EntityFramework when the database is Postgres.
dotnet add package MagicCSharp.Data.Postgres
Registration
builder.Services.AddPostgresDbContextFactory<MagicShopContext>(builder.Configuration);
Reads DB_HOST, DB_PORT, DB_NAME, DB_USER and DB_PASSWORD. Each is required — a missing one throws
rather than quietly defaulting to localhost. Set a different ConfigPrefix on PostgresConnectionOptions to point
a second context at another database (VECTOR_DB_HOST, and so on).
Registers a pooled factory, which is what makes the repositories' context-per-operation cheap: pooling
reuses the context and its internal service provider rather than rebuilding the model each time. Opens one
connection during registration, so a wrong host or password fails at startup instead of on the first request
that needs the database; set DB_VERIFY_CONNECTION=false where that is wrong — a test that swaps every
repository out, or a container that starts before its database.
Pool sizes, timeouts and retry live in PostgresConnectionOptions. Size MaxPoolSize against the server's
limit divided by the number of instances you run, or a rolling deploy will exhaust the server.
Two hooks for the provider, also on PostgresConnectionOptions: ConfigureDataSource for an Npgsql type plugin
such as pgvector's UseVector(), and ConfigureNpgsql for the options inside UseNpgsql. A query that Includes two collections at once
throws rather than multiplying the rows — that is almost never what was meant, and throwing turns a silent
performance cliff into a fix-once error.
Migrations
public class MagicShopContextFactory() : MagicDbContextFactory<MagicShopContext>("shop");
dotnet ef builds the context without your DI container, so it needs this. Without one it falls back to
booting the whole host just to read a connection string. The argument is the default database name; host,
port, user and password default to a local Postgres and are overridden by the same DB_* variables.
The UTC interceptor
Registered for you. It normalizes every DateTimeOffset command parameter to UTC.
MagicDbContext already does that on save, but save only sees entities. A Where comparing a timestamp, an
ExecuteUpdate, an ExecuteDelete and raw SQL all bind parameters without going near the change tracker.
Npgsql requires offset zero for a timestamptz parameter, so one of those carrying a local offset throws at
bind time — on a developer's machine, and not in CI, because CI runs in UTC.
Related packages
- MagicCSharp.Data.EntityFramework — the repository base classes this wires up
- MagicCSharp.Testing.Database — repository tests against a real PostgreSQL
The whole picture, and the optional repository layout: github.com/MagicDoorInc/MagicCSharp. MIT.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. 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. |
-
net9.0
- MagicCSharp.Data.EntityFramework (>= 1.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MagicCSharp.Data.Postgres:
| Package | Downloads |
|---|---|
|
MagicCSharp.Testing.Database
Repository integration testing for MagicCSharp against a real PostgreSQL in Testcontainers: one container shared across the suite, a logical database per test class, schema created once, and tables truncated between tests. Separate from MagicCSharp.Testing because it needs Docker, xUnit and Npgsql. |
GitHub repositories
This package is not used by any popular GitHub repositories.