我不能简单地注入数据库上下文,所以我必须依赖注入
IServiceScopeFactory
并像这样从注册服务中获取它。
AppDbContext Context()
{
using IServiceScope scope = Services.CreateScope();
return scope.ServiceProvider.GetRequiredService<AppDbContext>();
}
当我像这样在其他地方调用它时它不起作用
async Task Seed(int count)
{
AppDbContext context = Context();
...
await context.AddAsync(new Thing { ... });
await context.SaveChangesAsync();
}
根据报错信息,处理服务。而且,事实上,因为我将它设置在
using
语句的范围内,所以随着方法的结束,它应该是预期的。我看到两种处理方法。
我不喜欢他们两个。第一个导致代码冗余。另一个可能会阻止其他范围内的数据库访问。
处理它的更好选择是什么?
最初,我尝试在我的班级内部创建选项并创建数据库访问。但是,当我开始创建迁移时,它停止工作了,因为
dotnet ef migrations add Init
需要在Program
中注册服务,因此迫使我使用上面的方法获取它。
DbContextOptions<AppDbContext> ContextOptions()
{
DbContextOptionsBuilder<AppDbContext> builder = new();
builder.UseInMemoryDatabase("memento");
//builder.UseSqlServer(a => { });
return builder.Options;
}