-1

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?

1 Answer 1

0

It's not clear if the API application and the DB are in the same container, or if you're using a shell inside the container.

I assume you're invoking the dotnet ef command from your development machine, not from within a container.

A solution would be to generate a SQL script instead of updating the database directly using the following command:

bashdotnet ef migrations script -p MyProject1 -s MyProject2 -o initial-create.sql

Once the initial-create.sql file is generated, you can apply it to the database using the psql CLI or a tool like DBeaver.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.