Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate xxx

解决办法

由于底层类的构造函数依赖的是DbContext类,注入的是MyDbContext类,所以无法解析DbContext.需多加一行下面红色的代码

// 注册DbContext

services.AddDbContext<MyDbContext>(options => options.UseDm(Configuration.GetConnectionString("TestDMDB")));
services.AddScoped<DbContext, MyDbContext>(); //表明MyDbContext是DbContext的具体实现

详解:

https://stackoverflow.com/questions/46320189/asp-net-core-2-unable-to-resolve-service-for-type-microsoft-entityframeworkcore

作者:xuejianxiyang
出处:http://xuejianxiyang.cnblogs.com
关于作者:Heaven helps those who help themselves.
本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/xuejianxiyang/p/15131372.html