概述
net core 新搭建基架或操作数据库迁移时报错:
Unable to create an object of type 'SchoolContext'. Add an implementation of 'IDesignTimeDbContextFactory<SchoolContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
查找相关资料,解决办法如下:
在Data文件夹,创建一个名称为:SchoolContextFactory 的类,类代码如下:
namespace ContosoUniversity.Data
{
public class SchoolContextFactory:IDesignTimeDbContextFactory<SchoolContext>
{
// IConfiguration Configuration { get; } //使用Configuration 获取不到GetConnectionString("SchoolContext")。不能用
public SchoolContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<SchoolContext>();
// optionsBuilder.UseSqlServer(Configuration.GetConnectionString("SchoolContext"));
optionsBuilder.UseSqlServer("Server = (localdb)\mssqllocaldb; Database = SchoolContext; Trusted_Connection = True; MultipleActiveResultSets = true");
return new SchoolContext(optionsBuilder.Options);
}
}
}
注意: 红字内容。
最后
以上就是忧郁大侠为你收集整理的Unable to create an object of type 'SchoolContext'. Add an implementation of的全部内容,希望文章能够帮你解决Unable to create an object of type 'SchoolContext'. Add an implementation of所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复