EF Core on PostgreSQL with Npgsql — the provider specifics on top of `efcore-dotnet`. Use when mapping or querying a Postgres store from EF Core: optimistic concurrency, date and time columns, the legacy timestamp switch, precision, schemas, indexes, or the Npgsql registration. It encodes: xmin as a shadow row version, the legacy timestamp switch with `timestamp without time zone` kept on purpose, microsecond precision, one schema per bounded context, and indexes only on the id columns that q...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Efcore Postgres Dotnet?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jorgecostamacia-efcore-postgres-dotnet-code-harness)
---
name: efcore-postgres-dotnet
description: >-
EF Core on PostgreSQL with Npgsql — the provider specifics on top of
`efcore-dotnet`. Use when mapping or querying a Postgres store from EF Core:
optimistic concurrency, date and time columns, the legacy timestamp
switch, precision, schemas, indexes, or the Npgsql registration. It
encodes: xmin as a shadow row version, the legacy timestamp switch with
`timestamp without time zone` kept on purpose, microsecond precision, one
schema per bounded context, and indexes only on the id columns that
queries filter or join on and for uniqueness rules.
---
# EF Core on PostgreSQL (Npgsql)
The Postgres specifics of `efcore-dotnet`, through `Npgsql.EntityFrameworkCore.PostgreSQL`: `options.UseNpgsql(connectionString)`. The code that registers the context reads the connection string and requires it at startup.
## Optimistic concurrency — xmin
When writes to a row can race (concurrent consumers, see `messaging`), guard them with Postgres's own row version:
```csharp
entity
.Property<uint>("RowVersion")
.IsRowVersion();
```
- **A shadow property over `xmin`**: every Postgres row has it, so there is no column to add, nothing in the aggregate and no migration.
- A save over a row that another writer changed after the read throws `DbUpdateConcurrencyException`. Let it propagate: the bus retries the message, and the use case decides again against the row as it now is. No retry loop in the service.
- It works because the operation reads and writes through one scoped context, so the tracker holds the original `xmin`.
- Only where writes can actually race. A table written by one sequential process doesn't need it.
## Dates and times
- **The legacy timestamp switch stays**: `AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true)` in `Program.cs`, before the code builds the host, and every date column mapped as **`HasColumnType("timestamp without time zone")`**. A move to `timestamptz` means a migration of live columns: that is the end state, deferred on purpose. Don't mix models: new date columns follow the same convention.
- Without the switch, Npgsql refuses to write a `DateTime` of `Kind.Utc` into a `timestamp without time zone` column. With the switch, a read comes back `Unspecified`. So the columns that this layer writes in UTC get the kind converter on the mapping (see `efcore-dotnet`).
- **Postgres keeps microseconds (6 decimal places); .NET carries 100 ns (7).** Postgres drops the seventh decimal of every date that the code writes, or sends as a parameter, so the date read back is not the date written. So truncate the dates that the code compares (a version guard, a sweep) to microseconds before you store them. Otherwise a redelivered `…4567` looks newer than the stored `…4560`. The truncation lives in the value object's conversion (see `ddd-dotnet`).
- Test fixtures set the same switch before the first connection, or the suite runs under different rules than production.
## Schemas and names
- **One schema per bounded context**, never `public`: `ToTable("pedidos", "<schema>")`. Tables and columns in snake_case, constraints named (`pk_pedidos`).
- The four state scripts are idempotent (`CREATE … IF NOT EXISTS`): `schema.sql`, `tables.sql`, `indexes.sql`, and `seed.sql` for the fixed rows that the service needs (`INSERT … ON CONFLICT DO NOTHING`; a comment only, when it needs none). Then `migrations/`, each migration an `ALTER TABLE … ADD COLUMN IF NOT EXISTS …`. The order and who runs them: `efcore-dotnet`.
- **String columns are `text`.** Postgres stores `text` and `varchar(n)` the same way. A bound in the type is a rule that the store enforces, with a `22001` instead of the typed `ValidationException`. A change to the bound is an `ALTER`. The rule lives in the value object's validator, and there is no `HasMaxLength` on a Postgres mapping — with an explicit `text` it does nothing.
## Indexes
**Index the id columns that queries filter or join on, and the unique indexes that back a uniqueness rule. Any other index needs a measured plan.** An id column gets its index whether or not a foreign key declares it. Use plain ascending `(col)` indexes. A unique index is the guarantee behind a uniqueness rule that the use case also checks (see `validation-dotnet`). Measurements showed that indexes on flag columns (`meta_active`) are useless: if most rows share a value, the planner scans anyway. Do not add them. Any other index needs a measured query plan that uses it.