在单独的Docker容器中使用数据库时的Add-Migration引发错误 [英] Add-Migration while using database in separate Docker container throws an error

查看:304
本文介绍了在单独的Docker容器中使用数据库时的Add-Migration引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用自动生成的身份设置创建了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:\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

有些事情我在这里不明白。第一个错误将我指向 C:\root\.nuget\packages\microsoft.aspnetcore.identity.ui\3.1.1\staticwebassets\V4\ 不存在。然后第二个错误告诉我 ApplicationDbContext 无法创建。链接指向指向以下网页:

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:


在创建新的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迁移添加来解决该问题命令,但在所有情况下,都应将此问题报告给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容器中使用数据库时的Add-Migration引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆