c# - Docker:dotnet ef数据库更新失败

标签 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"]

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

最佳答案

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

  • 必须根据以下站点重组我的Program.cs:https://www.ryadel.com/en/buildwebhost-unable-to-create-an-object-of-type-applicationdbcontext-error-idesigntimedbcontextfactory-ef-core-2-fix/
  • 我必须创建一个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);
        }
    }
    
  • 在我的Dockerfile中,我必须在.csproj文件旁边显式复制appsettings.json。在我的情况下,这意味着将此命令添加到Dockerfile中:
  • COPY ["src/appsettings.json", "WebApp/FileManager.WebApp/"]
    完成这些步骤后,部署成功。

    关于c# - Docker:dotnet ef数据库更新失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968469/

    相关文章:

    docker - 为什么我得到一个没有 IP 地址的 docker 容器?

    c# - 锁定 Dictionary.Count

    c# - 使用 jquery 将行添加到客户端转发器内的表中

    c# - 在 Azure 上解析 html 文件

    c# - 在附加到新的父 Visual 之前,必须断开指定子项与当前父 Visual 的连接

    c# - Entity Framework 数据注释对 DDD 和关注点分离有害吗?

    c# - Entity Framework 中的 HierarchyID 不起作用

    c# - Entity Framework ..自引用表..获取深度=x的记录?

    docker - Hyperledger Composer 设置 connection.json

    Docker 容器(用于 Elasticsearch )每次启动和退出都出现错误 "[Qkskaso] failed to read local state, exiting..."