I have dockerized API & PostgreSQL database, when I locally execute
dotnet ef migrations add InitialCreate -p MyProject1 -s MyProject2
It successfully creates initial migration.
When I try to update DB via
dotnet ef database update -p MyProject1 -s MyProject2
I get error, because it cannot communicate with Docker and it tries to update it locally (I assume) - error message:
...The ConnectionString property has not been initialized.
However, in both of my appsettings.json I have ConnectionStrings: "" just empty, and use environment variables:
services:
api:
build:
context: ../../
dockerfile: environments/dev/Dockerfile
ports:
- "5000:5000"
environment:
- ConnectionStrings__DefaultConnection=User ID=${POSTGRES_USER};Password=${POSTGRES_PASSWORD};Server=${POSTGRES_SERVER};Port=${POSTGRES_PORT};Database=${POSTGRES_DB};
- ASPNETCORE_URLS=http://+:5000
- ASPNETCORE_ENVIRONMENT=Development
- DOTNET_USE_POLLING_FILE_WATCHER=1
volumes:
- ../../:/src
working_dir: /src/API
env_file:
- ../../.env
depends_on:
- db
restart: always
command: dotnet watch --no-hot-reload --project API.csproj run --urls=http://0.0.0.0:5000
db:
image: postgres:latest
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ports:
- "5432:5432"
restart: always
env_file:
- ../../.env
volumes:
- app_data:/var/lib/postgresql/data
volumes:
app_data:
How to handle migrations when using dockerized PostgreSQL?