我希望具有以下结构
一个Repository
注入了DbContext
。
Controller
注入了Repository
。
因此,控制器使用存储库,并且存储库中已包含上下文。
所以我在startup.cs
services.AddDbContext<DbContext> x => x.UseSqlServer(Configuration.Getvalue<string>("AppSettings:DBCS")));
在上述行正下方注册回购
services.AddScoped(x => new DB.Tasker.LogEntryRepo());
,然后在控制器内部使用DIed存储库
private LogEntryRepo _repo;
public LogsController(LogEntryRepo repo)
{
_repo = repo;
}
问题是编译器抱怨这行services.AddScoped(x => new DB.Tasker.LogEntryRepo());
很可疑,因为仓库显然没有无参数控制器。
完成我要完成的工作的正确方法是什么?