.NET Core 5 集成Npgsql.EntityFrameworkCore.PostgreSQL时迁移与API启动报错求助
Let’s break down how to resolve this connection error step by step—this is a common issue when switching from SQLite to PostgreSQL with EF Core, and most of the time it boils down to simple configuration mistakes.
1. Double-Check Your PostgreSQL Port
Looking at your connection strings, you’ve set Port=54321, but PostgreSQL’s default port is 5432. This is almost certainly the main cause of that stream read error—your app is trying to connect to a port where PostgreSQL isn’t listening.
Update your connection strings to use the correct port:
"ConnectionStrings": { "DefaultConnection": "Server=localhost;Port=5432;User Id=appuser;Password=secret;Database=ecocampo", "IdentityConnection": "Server=localhost;Port=5432;User Id=appuser;Password=secret;Database=identity", "Redis": "localhost" }
Note: If you intentionally changed PostgreSQL’s port to 54321, skip this step and confirm the service is actually running on that port instead.
2. Confirm PostgreSQL is Running Locally
Even if the port is correct, if PostgreSQL isn’t running, your app can’t connect. Here’s how to check on macOS (since your path shows /Users/):
- Run
brew services listin your terminal—look forpostgresqland make sure its status isstarted. - If it’s stopped, start it with
brew services start postgresql. - On Windows, check the Services manager for "postgresql-x64-xx" (xx is your version) and ensure it’s running.
3. Validate Connection String Syntax
Make sure there are no typos or extra spaces in your connection string. Npgsql is strict about formatting:
- Avoid spaces around parameter names/values (e.g.,
Port= 5432will cause issues). - Ensure
User Id,Password, andDatabaseare spelled correctly.
4. Match Npgsql EF Core Version to .NET 5
Since you’re using .NET Core 5, you need to use a 5.x version of Npgsql.EntityFrameworkCore.PostgreSQL. Newer versions (6.x+) don’t support .NET 5, which can lead to unexpected connection errors.
Check your project’s .csproj file for the package reference:
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.17" />
If the version is wrong, install the correct one with:
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 5.0.17
5. Test Connection Directly with psql
Let’s rule out EF Core entirely and test if you can connect to PostgreSQL directly. Run this command in your terminal:
psql -h localhost -p 5432 -U appuser -d ecocampo
- If this fails, you know the problem is with PostgreSQL itself (not EF Core). Common issues here include:
- The
appuserdoesn’t have permission to access the database. - The
ecocampodatabase hasn’t been created yet.
- The
- To fix missing databases, create them via
psql:CREATE DATABASE ecocampo; CREATE DATABASE identity;
6. Clean up Existing Migrations (If Needed)
Since you switched from SQLite to PostgreSQL, your existing migration files are designed for SQLite and might cause conflicts. Delete the Migrations folder in your project, then re-run:
dotnet ef migrations add InitialCreate dotnet ef database update
After going through these steps, your API should connect to PostgreSQL without the stream read error, and migrations should run smoothly.
内容的提问来源于stack exchange,提问作者Bunnytheh0tbeast

