net core 新搭建基架或操作数据库迁移时报错:
Unable to create an object of type 'SchoolContext'. Add an implementation of 'IDesignTimeDbContextFactory<SchoolContext>' to the project, or see /?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);
}
}
}
注意: 红字内容。
本文发布于:2024-02-02 15:21:38,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170685849844676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |