[英]Docker: dotnet ef database update fails
I've just started using EF on my project, and the docker ef database update
command runs perfectly, when I run it locally from the project's folder. 我刚刚开始在项目上使用EF,并且当我从项目文件夹中本地运行
docker ef database update
命令时,它可以完美运行。
However, when I am trying to deploy the application using docker, it fails at: 但是,当我尝试使用docker部署应用程序时,它在以下位置失败:
RUN dotnet ef database update
I get the following error message: 我收到以下错误消息:
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 looks like this (default): 我的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>();
}
My dockerfile looks like this: 我的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"]
What am I doing wrong? 我究竟做错了什么? It successfully deploys if I remove the database update.
如果删除数据库更新,它将成功部署。
Okay, so here are the steps I needed to do in order to fix my problem: 好的,这是我需要解决的步骤:
Had to restructure my Program.cs according to this site: https://www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core-2-fix/ 必须根据以下站点重组我的Program.cs: https : //www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core- 2修复/
I had to create a DesignTimeDbContextFactory, which creates the db context using the DefaultConnection
string found in appsettings.json
The factory looks like this: 我必须创建一个DesignTimeDbContextFactory,它使用
appsettings.json
的DefaultConnection
字符串创建db上下文。工厂看起来像这样:
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<FileManagerDbContext>
{
public FileManagerDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<FileManagerDbContext>();
var connectionString = configuration.GetConnectionString("DefaultConnection");
builder.UseSqlite(connectionString);
return new FileManagerDbContext(builder.Options);
}
}
appsettings.json
next to the .csproj file. appsettings.json
。 In my case this meant adding this command to the Dockerfile: COPY ["src/appsettings.json", "WebApp/FileManager.WebApp/"]
After these steps the deployment was successful. 完成这些步骤后,部署成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.