Reputation: 231
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.
However, when I am trying to deploy the application using docker, it fails at:
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):
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:
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.
Upvotes: 5
Views: 9087
Reputation: 231
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 in appsettings.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);
}
}
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.
Upvotes: 2