Get started with the PostgreSQL EF Core integrations
PostgreSQL is a mature, open-source object-relational database with a strong reputation for reliability, feature richness, and performance. The Aspire PostgreSQL EF Core integration lets .NET apps use Entity Framework Core to interact with a PostgreSQL database modeled in your AppHost using the PostgreSQL Hosting integration.
Why use PostgreSQL with EF Core and Aspire
Section titled “Why use PostgreSQL with EF Core and Aspire”Combining PostgreSQL, EF Core, and Aspire in your solution gives you:
- Zero-config local development. Aspire runs PostgreSQL from the
docker.io/library/postgrescontainer image with credentials generated automatically for you. - Strongly-typed data access. EF Core’s
DbContextmaps your C# model classes to PostgreSQL tables, with full LINQ query support and compile-time safety. - Automatic dependency injection. A single
AddNpgsqlDbContext<T>call registers yourDbContextsubclass in the DI container — no manual connection-string wiring. - Built-in health checks. The integration registers an EF Core
CanConnectAsynchealth check so your orchestrator can tell when the database is reachable. - OpenTelemetry out of the box. Logging, distributed tracing, and metrics are wired up automatically for both EF Core and the underlying Npgsql driver.
- EF migrations support. Use the standard
dotnet ef migrationsworkflow; Aspire handles the runtime connection details.
How the pieces fit together
Section titled “How the pieces fit together”The PostgreSQL EF Core integration has two sides: a hosting integration used in your AppHost to model the database resource, and an EF Core client integration used in each consuming .NET app.
architecture-beta group apphost(server)[AppHost] group consumer(server)[Consuming app] service hosting(server)[Postgres hosting] in apphost service db(database)[postgresdb] in apphost service efcore(server)[EF Core client] in consumer service app(server)[App] in consumer hosting:R --> L:db db:R --> L:efcore efcore:R --> L:app
The hosting integration lives in your AppHost project and models the PostgreSQL server and databases as resources. The EF Core client integration lives in each consuming .NET app and uses the connection information Aspire injects to register a DbContext through dependency injection.
Getting there is a two-step process: model PostgreSQL in your AppHost, then connect from your consuming app using EF Core.
-
Model PostgreSQL in your AppHost
Section titled “Model PostgreSQL in your AppHost”Add the PostgreSQL hosting integration to your AppHost, then declare a PostgreSQL server, one or more databases, and reference them from the apps that need to talk to the database. The PostgreSQL Hosting integration reference covers every capability — adding databases, pgAdmin, pgWeb, data volumes, init scripts, and more.
Set up PostgreSQL in the AppHost
-
Connect from your consuming app using EF Core
Section titled “Connect from your consuming app using EF Core”Install the
Aspire.Npgsql.EntityFrameworkCore.PostgreSQLpackage and callAddNpgsqlDbContext<T>to register yourDbContextsubclass. See Connect to PostgreSQL with EF Core for the full reference — connection properties, keyed services, configuration providers, health checks, and telemetry.Connect to PostgreSQL with EF Core