错误详情:
这个错误曾经困扰了我整整一个下午,百度了好久好久.....也使用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);
}
}
最终创建了迁移
引用的网址: