如何在ASP.NET Core 2.1中使用IDesignTimeDbContextFactory实现?

生命力

我有ASP.NET Core 1.1项目,我正在尝试将其转换为v2.1。我已经找到IDesignTimeDbContextFactory实现,但是我不知道应该在哪里创建该工厂的实例以及如何将其用于正常迁移。现在,在创建/删除迁移之前,我必须清理并重建解决方案,因为如果没有,我将收到以下消息:Unable to create an object of type 'MyDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time所有上下文数据都在单独的项目中。如何使用IDesignTimeDbContextFactory实现或我做错了什么?

看到

将此类添加到具有DbContext的文件夹中。

public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
    {      

        MyDbContext IDesignTimeDbContextFactory<MyDbContext>.CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<MyDbContext>();
            var connectionString = configuration.GetConnectionString("DefaultConnection");

            builder.UseSqlServer(connectionString);

            return new MyDbContext(builder.Options);
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Asp.net Core 2-如何在Asp.net Core 2.0中使用ServiceLocator

如何在ASP.net Core 2中使用Windsor IoC

如何在asp.net core中实现经典的webapi 2路由

如何在ASP.NET Core 2中引发ForbiddenException,而不是使用AccessDeniedPath

如何在ASP.NET Core 2.0中实现machineKey

如何在 ASP.NET Core 中实现设置页面?

如何在 ASP .NET Core 中实现基于 slug 的路由?

如何在Asp.net Core rc1中的静态方法中使用DI

如何在 Jetbrains Rider 中為 ASP.NET CORE 2 設置 XUnit 測試?

如何在ASP.NET Core 2中对Facebook Web API进行身份验证

如何在发布中包含asp.net Core 2依赖项

如何在ASP.NET Core RC2中制作Websocket服务器?

如何在TFS中构建ASP.NET Core 2应用

如何在ASP.NET Core 2.0中为EFCore 2配置SQL的日志记录

如何在ASP.NET Core 2中添加身份验证Cookie超时

如何在ASP.NET Core 2中为Steam配置OpenID Connect?

如何在ASP.NET Core 2中更改CreatedAtAction的路由格式?

如何在ASP.NET Core 2.x中以会话状态存储和检索对象?

如何在ASP.net Core 2中创建动态API

如何在ASP.NET Core中使用Bootstrap 4

如何在ASP.NET Core应用中使用HttpClient

如何在 ASP.Net Core 3 中使用 UseNodeModules?

如何在ASP.NET Core中使用jquery

如何在ASP.NET Core中使用SqlClient?

如何在ASP.NET Core中使用区域

如何在ASP.NET Core中使用npm

如何在 Asp.Net Core 中使用 Ninject?

如何在 ASP.NET Core 中使用 API?

如何在 ASP.NET Core 中使用异步“ForEach”?