问题描述
我正在更新 DbContext 的注册方式。在单元测试中,注册内存数据库的旧方法如下
builder.RegisterInstance(new DbContextOptionsBuilder<OneContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
builder.RegisterType<OneContext>().InstancePerDependency();
我使用 AddDbContext 的新方法
services.AddDbContext<OneContext>(options => options
.UseInMemoryDatabase(Guid.NewGuid().ToString()),ServiceLifetime.Transient);
在将其注册为 InstancePerDependency 进行解析时,我使用了每个上下文的作用域,如下文中的一篇文章所述。
using (ILifetimeScope scope = container.BeginLifetimeScope("KEY"))
{
scope.Resolve<IService>(); // Instance #1
using (ILifetimeScope childScope = scope.BeginLifetimeScope())
{
childScope.Resolve<DbContext>();
childScope.Resolve<IService>();
}
}
我在这里做错了什么?你介意告诉我吗。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)