在单独的 Docker 容器中使用数据库时添加迁移会引发错误 [英] Add-Migration while using database in separate Docker container throws an error
问题描述
我已经使用自动生成的身份设置创建了 dockerized ASP.net Core 应用程序(所以我已经有了默认的实体框架配置).我已经将 SQL Server 创建为单独的容器,这是我的 docker-compose.yml
:
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"
我已经测试了数据库配置:由于 Visual Studio 为我生成了身份设置,我可以成功地创建新用户并且他们被正确地存储在内存中.
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; }
}
为了应用这些更改,我在包管理器控制台中使用了 Add-Migration WeightLogger
.
To apply those changes, I've used Add-Migration WeightLogger
in Package Manager Console.
这是一个输出:
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:
oot.nugetpackagesmicrosoft.aspnetcore.identity.ui3.1.1staticwebassetsV4
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
这里有几件事我不明白.第一个错误将我指向不存在的 C:
oot.nugetpackagesmicrosoft.aspnetcore.identity.ui3.1.1staticwebassetsV4
.然后是第二个错误告诉我无法创建 ApplicationDbContext
.链接指向我的网页,内容如下:
There are a couple of things I don't understand here. The first error points me to C:
oot.nugetpackagesmicrosoft.aspnetcore.identity.ui3.1.1staticwebassetsV4
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:
当你创建一个新的 ASP.NET Core 应用程序时,这个钩子是默认包含的.
When you create a new ASP.NET Core application, this hook is included by default.
那么,这是否意味着我不需要配置任何东西?为什么它不工作呢?使用 Docker 容器作为环境时,我是否应该以不同的方式创建迁移?
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?
推荐答案
我遇到了完全相同的问题,最终通过从 Visual Studio 中删除 docker-compose 项目并保留 docker-compose.yaml 并管理它通过命令行并解决它.
I got exactly the same issue and I ended up solving it by deleting the docker-compose project from visual studio and just keep the docker-compose.yaml and manage it through the command line and that solve it.
对于那些不想删除解决方案的docker-compose支持并面临同样问题的人,我认为可以通过为上下文实现设计时接口或使用docker来解决它ef migrations add
命令,但在所有情况下,这是一个应该报告给 Microsoft 的问题.
For those who don't want to remove the docker-compose support for the solution and facing the same issue, I think it is possible to solve it by implementing a design-time interface for the context or using docker ef migrations add
command but in all cases, it's an issue that should be reported to Microsoft.
这篇关于在单独的 Docker 容器中使用数据库时添加迁移会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!