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);
        }

    }
}

注意: 红字内容。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值