我是靠谱客的博主 忧郁大侠,这篇文章主要介绍Unable to create an object of type 'SchoolContext'. Add an implementation of,现在分享给大家,希望可以做个参考。

 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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部