Docker:dotnet ef 数据库更新失败

J. *_*Loe 5 c# entity-framework docker .net-core asp.net-core

我刚刚开始在我的项目中使用 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
Run Code Online (Sandbox Code Playgroud)

我的 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>();
}
Run Code Online (Sandbox Code Playgroud)

我的 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"]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如果我删除数据库更新,它会成功部署。

J. *_*Loe 2

好的,以下是解决我的问题所需执行的步骤:

  1. 必须根据此网站重组我的 Program.cs:https://www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core- 2-修复/

  2. 我必须创建一个 DesignTimeDbContextFactory,它使用 ThefactoryDefaultConnection中找到的字符串创建数据库上下文,如下所示:appsettings.json

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);
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 在我的 Dockerfile 中,我必须显式复制appsettings.json到 .csproj 文件旁边。就我而言,这意味着将此命令添加到 Dockerfile 中:

COPY ["src/appsettings.json", "WebApp/FileManager.WebApp/"]

经过这些步骤,部署成功。