我刚刚开始在项目中使用EF,当我从项目文件夹本地运行docker ef database update
命令时,它运行得非常完美。
但是,当我尝试使用Docker部署应用程序时,它在以下位置失败:RUN dotnet ef database update
我收到以下错误消息:
An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: The configuration file 'appsettings.json' was not found and is not optional. The physical path is '/src/WebApp/FileManager.WebApp/bin/Debug/netcoreapp2.2/appsettings.json'.
Unable to create an object of type 'FileManagerDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
The command '/bin/sh -c dotnet ef database update' returned a non-zero code: 1
My Program.cs如下所示(默认):
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
我的dockerfile看起来像这样:
FROM microsoft/dotnet:2.2.0-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.2.103-sdk AS build
WORKDIR /src
COPY ["src/filemanager.csproj", "WebApp/FileManager.WebApp/"]
RUN dotnet restore "WebApp/FileManager.WebApp/filemanager.csproj"
WORKDIR /src/WebApp/FileManager.WebApp
COPY . .
RUN dotnet build "filemanager.csproj" -c Release -o /app
RUN dotnet ef database update
FROM build AS publish
RUN dotnet publish "filemanager.csproj" -c Release -o /app
FROM node:11.6.0-alpine as nodebuild
WORKDIR /src
COPY ["src/client", "ClientApp/"]
WORKDIR /src/ClientApp
RUN yarn
RUN yarn build
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY --from=nodebuild /src/ClientApp/build /app/wwwroot
ENTRYPOINT ["dotnet", "filemanager.dll"]
我做错了什么?如果我删除数据库更新,它将成功部署。
1条答案
按热度按时间oxf4rvwz1#
好的,下面是我需要执行的步骤,以解决我的问题:
1.不得不根据这个网站重组我的Program.cs:https://www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core-2-fix/
1.我必须创建一个DesignTimeDbContextFactory,它使用
appsettings.json
中的DefaultConnection
字符串创建db上下文。1.在我的Dockerfile中,我必须显式地将
appsettings.json
复制到.csproj文件旁边,在我的例子中,这意味着将以下命令添加到Dockerfile中:COPY ["src/appsettings.json", "WebApp/FileManager.WebApp/"]
完成这些步骤后,部署成功。