Docker: dotnet ef database update fails Docker: dotnet ef database update fails docker docker

Docker: dotnet ef database update fails


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/

  2. I had to create a DesignTimeDbContextFactory, which creates the db context using the DefaultConnection string found in appsettings.jsonThe factory looks like this:

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. 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.