Docker:dotnet ef数据库更新失败

时间:2019-03-03 11:47:53

标签: c# entity-framework docker asp.net-core .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

我的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 个答案:

答案 0 :(得分:0)

好的,这是我需要解决的步骤:

  1. 必须根据以下站点重新构建我的Program.cs:https://www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core-2-fix/

  2. 我必须创建一个DesignTimeDbContextFactory,它使用DefaultConnection中的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);
    }
}
  1. 在我的Dockerfile中,我必须在.csproj文件旁边显式复制appsettings.json。就我而言,这意味着将以下命令添加到Dockerfile中:

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

完成这些步骤后,部署成功。