L
L
lucky42021-05-13 16:14:00
ASP.NET
lucky4, 2021-05-13 16:14:00

How to update database in Docker?

There is an API application that works with the database and with EF Core, on migrations.
The application has been moved to a docker container. All its dependencies and mssql database have been moved to a separate container.
The application is launched on the port, swagger shows all methods, models, etc.

BUT! When after going to some controller with GET method. An error is thrown:

Failed executing DbCommand (56ms) [Parameters=[@__p_0='?' (DbType = Int32), @__p_1='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
web      | SELECT [t].[Id], [t].[Description], [t].[Label], [t].[Price], [t].[Thumbnail], [t0].[ProductId], [t0].[CategoryId], [t0].[Id], [t0].[Label], [t0].[Thumbnail]
web      | FROM (
web      |     SELECT [p].[Id], [p].[Description], [p].[Label], [p].[Price], [p].[Thumbnail]
web      |     FROM [Products] AS [p]
web      |     ORDER BY [p].[Id]
web      |     OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY
web      | ) AS [t]
web      | LEFT JOIN (
web      |     SELECT [p0].[ProductId], [p0].[CategoryId], [c].[Id], [c].[Label], [c].[Thumbnail]
web      |     FROM [ProductCategories] AS [p0]
web      |     INNER JOIN [Categories] AS [c] ON [p0].[CategoryId] = [c].[Id]
web      | ) AS [t0] ON [t].[Id] = [t0].[ProductId]
web      | ORDER BY [t].[Id], [t0].[ProductId], [t0].[CategoryId], [t0].[Id]
web      | [13:05:29 ERR] An exception occurred while iterating over the results of a query for context type 'ShoppingCart.Infrastructure.Data.DataContext'.
web      | Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Products'.


As I understand it, the database itself exists, because connection has occurred. But, no data and no table. * copied from the solution and will build the project where there are migrations.

Question: How can I "forcibly" update the database then? I know that there are dotnet cli commands like dotnet ef database update and so on. But I don’t understand how and where to add it)

DOCKERFILE
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 ["ShoppingCart.API/ShoppingCart.API.csproj", "ShoppingCart.API/"]
RUN dotnet restore "ShoppingCart.API/ShoppingCart.API.csproj"
COPY . .
WORKDIR "/src/ShoppingCart.API"
RUN dotnet build "ShoppingCart.API.csproj" -c Release -o /app/build

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

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

FROM base AS final
WORKDIR /migration
COPY --from=migration /app/migration .

WORKDIR /app
COPY --from=build /app/build .
ENTRYPOINT ["dotnet", "ShoppingCart.API.dll"]


docker-compose
version: '3.9'

networks:
  localdev:
   name: localdev

services:
  web:
   container_name: web
   build: .
   depends_on: [ mssql ]
   ports:
    - "8080:80"
   networks:
    - localdev

  mssql:
   image: "mcr.microsoft.com/mssql/server"
   container_name: mssql
   hostname: mssql
   environment:
    SA_PASSWORD: "fA0bD7rO3iE1mU0w"
    ACCEPT_EULA: "Y"
   restart: unless-stopped
   networks:
    - localdev
   ports:
    - "1433:1433"
   expose:
    - 1433

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-05-13
@vabka

Question: How can I "forcibly" update the database then? I know that there are dotnet cli commands like dotnet ef database update and so on. But I don’t understand how and where to put it)

DbContext.Database.EnsureCreated()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question