You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 5 集成Npgsql.EntityFrameworkCore.PostgreSQL时迁移与API启动报错求助

Fixing Npgsql Stream Error with EF Core 5 and PostgreSQL

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 list in your terminal—look for postgresql and make sure its status is started.
  • 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= 5432 will cause issues).
  • Ensure User Id, Password, and Database are 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 appuser doesn’t have permission to access the database.
    • The ecocampo database hasn’t been created yet.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:32:43