'Dependency Injection in ASP.NET Core 5.0 with Docker

Currently I am doing a project with ASP.NET Core 5.0 and using EF (Entity Framework) with Docker containers which I am trying to connect to SQL Server, but for some reason when trying to migrate all of the changes to SQL it does not get the Connection String.

Stepping through on debug I found that the value was populated.

In this Image, the connectionString property does contain the connection string from my secrets.json file

This Image, I've been trying to run migrations but I get an error

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.

This is my docker file that was generated with my project

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["CreateRandomDatabaseEntries.API/CreateRandomDatabaseEntries.API.csproj", "CreateRandomDatabaseEntries.API/"]
RUN dotnet restore "CreateRandomDatabaseEntries.API/CreateRandomDatabaseEntries.API.csproj"
COPY . .
WORKDIR "/src/CreateRandomDatabaseEntries.API"
RUN dotnet build "CreateRandomDatabaseEntries.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "CreateRandomDatabaseEntries.API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CreateRandomDatabaseEntries.API.dll"]

My project can communicate with my SQL Server with the creation of a docker compose project

version: '3.4'

services:
  createrandomdatabaseentries.api:
    image: ${DOCKER_REGISTRY-}createrandomdatabaseentriesapi
    build:
      context: .
      dockerfile: CreateRandomDatabaseEntries.API/Dockerfile

  ms-sql-server:
    image: mcr.microsoft.com/mssql/server:2019-latest
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: "MyPassword" 
    volumes:  
      - ./data/mssql:/var/opt/mssql3  
    ports:  
      - '1433:1433'  
    expose:  
      - 1433   

This Image shows the Connected Services are configured.

Trying to edit the SQL Server Database string and saving and then editing again does not retain value passed.

I am using Visual Studio 2019 Version 16.11.8 at the moment

The question:

How can I setup my package manager console to run the update-database or migrations to the docker container that I can experiment with more Database features through entity framework.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source