I've created dockerized ASP.net Core app with auto-generated identity setup (so I've already had by default entity framework configuration). I've created SQL Server as separate container, here is my docker-compose.yml
:
version: '3.4'
services:
blazorapp1:
image: blazorapp1
build:
context: .
dockerfile: Dockerfile
depends_on:
- db
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "[SOMEPASSWORD]"
ACCEPT_EULA: "Y"
I've tested database configuration: as Visual Studio generated identity setup for me, I could successfully create new users and they were properly stored in memory.
Then I wanted to create new database table:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public virtual DbSet<WeightLog> WeightLogs { get; set; }
}
To apply those changes, I've used Add-Migration WeightLogger
in Package Manager Console.
Here is an output:
PM> Add-Migration WeightLogger
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: C:\root\.nuget\packages\microsoft.aspnetcore.identity.ui\3.1.1\staticwebassets\V4\
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
There are a couple of things I don't understand here. The first error points me to C:\root\.nuget\packages\microsoft.aspnetcore.identity.ui\3.1.1\staticwebassets\V4\
which does not exist. Then there is the second error telling me that ApplicationDbContext
can't be created. Link points me to webpage that tells:
When you create a new ASP.NET Core application, this hook is included by default.
So, does this mean I shouldn't need to configure anything? Why it isn't working then? Should I create migrations differently when using Docker container as environment?
docker ef migrations add
commandupdate-database
command. I copied the package fromC:\Users\{Username}\.nuget\packages\microsoft.aspnetcore.identity.ui
toC:\root\.nuget\packages\microsoft.aspnetcore.identity.ui
. After doing that I was able to runupdate-database
and everything worked as expected.docker : docker: 'ef' is not a docker command.
dotnet ef migrations add
(link to documentation).