Within ConfigureServices I have
services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
as well as
services.AddSingleton<IMyModel>(s =>
{
var dbContext = s.GetService<MyContext>();
var lastItem= dbContext.Items.LastOrDefault();
return new MyModel(lastItem);
});
But s.GetService<MyContext>() throws an error:
Cannot resolve scoped service 'MyContext' from root provider.
How can I achieve that? I don't want to inject MyDbContext in MyModel contructor as it is in a library which should have no reason to know about Entity Framework.