简体   繁体   English

Docker:dotnet ef数据库更新失败

[英]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: 好的,这是我需要解决的步骤:

  1. 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修复/

  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.jsonDefaultConnection字符串创建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);
    }
}
  1. In my Dockerfile, I had to explicitly copy appsettings.json next to the .csproj file. 在我的Dockerfile中,我必须在.csproj文件旁边显式复制appsettings.json In my case this meant adding this command to the Dockerfile: 就我而言,这意味着将以下命令添加到Dockerfile中:

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

After these steps the deployment was successful. 完成这些步骤后,部署成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dotnet ef migrations {name} dotnet ef database update - dotnet ef migrations {name} dotnet ef database update ASP.NET dotnet -ef 数据库更新 - ASP.NET dotnet -ef database update “dotnet ef数据库更新”以编程方式与Entity Framework Core进行编程 - “dotnet ef database update” programatically with Entity Framework Core NpgsqlException:dotnet ef 数据库更新上的“未知消息代码:74” - NpgsqlException : “Unknown message code: 74” on dotnet ef database update 如何卸载 EF Core 表? 或者如何删除所有迁移? 或者如何从用户代码调用`dotnet ef database update 0`? - How to uninstall EF Core tables? Or how to remove all migrations ? Or how to call `dotnet ef database update 0` from user code? 如何调试 dotnet ef 数据库删除? - how to debug dotnet ef database drop? docker build 期间 dotnet 发布失败 - dotnet publish fails during docker build MySql上的EF Core`update-database`失败,`__EFMigrationsHistory&#39;不存在。 - EF Core `update-database` on MySql fails with `__EFMigrationsHistory' doesn't exist` | DataDirectory目录| 用于EF更新数据库 - |DataDirectory| for EF Update-Database EF 6.x AddorUpdate上下文在更新时失败 - EF 6.x AddorUpdate context fails on update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM