EntityframeworkCore创建数据库迁移时出现 Unable to create an object of type 'ApplicationDbContext'.

错误详情:

 

这个错误曾经困扰了我整整一个下午,百度了好久好久.....也使用Google搜索了一下,找到了一点相关的内容,但是还是没有解决问题。最终通过bing搜索到了这个问题的解决方案(果然亲儿子。。。其实没有什么联系啦...)问题描述如下:

Add-Migration CreateIdentitySchema   // 在PMC 中添加这行命令出现了以下错误

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

你需要创建一个Data文件夹,然后在里面创建一个名称为:DesignTimeDbContextFactory 的类,类代码如下:


 public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        //ApplicationDbContext 代表的是你的创建失败的那个类型
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
            var builder = new DbContextOptionsBuilder();
            builder.UseSqlServer("server=.;uid=sa;pwd=102489;database=IdentityDemo");
            return new ApplicationDbContext(builder.Options);
        }
    }

最终创建了迁移

引用的网址:

原网址

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值