添加“ IDesignTimeDbContextFactory< ServicesDbContext>”的实现.NET Core 2.0中创建迁移步骤时遇到的项目错误 [英] Add an implementation of 'IDesignTimeDbContextFactory<ServicesDbContext>' to the project error while creating a migration step in .NET core 2.0

查看:553
本文介绍了添加“ IDesignTimeDbContextFactory< ServicesDbContext>”的实现.NET Core 2.0中创建迁移步骤时遇到的项目错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET core 1.0 webapp正常工作。我必须升级到.NET Core 2.0。我还必须为SQLite数据库添加一个迁移步骤。

I had a .NET core 1.0 webapp working fine. I had to upgrade to .NET Core 2.0. I also had to add a migration step for my SQLite database.

如果我启动此命令:


Add-Migration MyMigrationStepName

Add-Migration MyMigrationStepName

我收到此错误:


无法创建类型为 ServicesDbContext的对象。在项目
中添加$ I $$ I $$ I $$$$$实现,或查看> ://go.microsoft.com/fwlink/?linkid = 851728 ,用于在设计时支持
的其他模式。

Unable to create an object of type 'ServicesDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

我在SO以及其他博客和网站上看到了很多答案,但是他们都没有真正说出在哪里实现这种接口以及具体方法应该包含什么代码!

I've seen plenty of answers on SO and on other blogs and website but none of them actually say where to implement such interface and what code the concrete method should contain!

推荐答案

示例

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

public class ApplicationContextDbFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    ApplicationDbContext IDesignTimeDbContextFactory<ApplicationDbContext>.CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer<ApplicationDbContext>("Server = (localdb)\\mssqllocaldb; Database = MyDatabaseName; Trusted_Connection = True; MultipleActiveResultSets = true");

        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

位置

将该类放在放置ApplicationDbContext的位置。当您使用dotnet ef cli命令时,它将自动被拾取。

Put that class where the ApplicationDbContext is put. It will then be automatically picked up when you use dotnet ef cli commands.

这篇关于添加“ IDesignTimeDbContextFactory&lt; ServicesDbContext&gt;”的实现.NET Core 2.0中创建迁移步骤时遇到的项目错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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