尝试迁移时 NpgsqlTimeout 超时

NpgsqlTimeout timeout when trying to migrate

我正在 .Net 5 中开发一个项目,使用的数据库是 Postgres。我正在使用 docker-compose 文件数据库,但是当我 运行 dotnet ef database update 命令时,出现以下错误:

Build started...
Build succeeded.
Npgsql.NpgsqlException (0x80004005): Exception while connecting
 ---> System.Net.Sockets.SocketException (111): Connection refused
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   ....
Exception while connecting

Docker-撰写:

version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: Guilherme
      POSTGRES_PASSWORD: XXXX
      POSTGRES_DB: BuiltCode

  pgadmin:
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - "5050:80"

连接字符串:

"PostgreSql": "Username=Guilherme;Password=XXXX;Server=localhost;Port=5432;Database=BuiltCode;"

有人可以帮我吗?

db 部分下添加:

ports:
- '5432:5432'