【发布时间】:2017-09-20 16:32:00
【问题描述】:
在ConfigureServices我有
services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
还有
services.AddSingleton<IMyModel>(s =>
{
var dbContext = s.GetService<MyContext>();
var lastItem= dbContext.Items.LastOrDefault();
return new MyModel(lastItem);
});
但是s.GetService<MyContext>() 抛出错误:
无法从根提供程序解析范围服务“MyContext”。
我怎样才能做到这一点?我不想在MyModel 构造函数中注入MyDbContext,因为它在一个没有理由知道Entity Framework 的库中。
【问题讨论】:
标签: c# asp.net-core entity-framework-core