Docker: dotnet ef database update fails
Okay, so here are the steps I needed to do in order to fix my problem:
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/
I had to create a DesignTimeDbContextFactory, which creates the db context using the
DefaultConnection
string found inappsettings.json
The 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); }}
- 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.