c# - 内存数据库注册 InstancePerDependency System.OutOfMemoryException
问题描述
我正在更新 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>();
}
}
我在这里做错了什么?你介意让我知道吗。
解决方案
推荐阅读
- react-native - 更新到 AndroidX 和 react-native 版本 0.59.10 后无法构建 react-native android 应用
- amazon-web-services - 所有实例的 Aws 成本和使用情况
- sql - 如何获取所有交易详情取决于开始和结束日期?
- node.js - 使用 mongoose 在特定日期查询 ISODate
- google-apps-script - 函数调用中不带引号的字符串
- javascript - TypeScript 的 Mongoose loadClass 问题
- css - 在ionic4的网格单元格中用一个字母居中一个圆圈
- javascript - 如何使用 javascript 制作小费计算器?
- amazon-web-services - 如何使用 boto3 为 S3 存储桶启用对象级日志记录
- google-analytics - 谷歌分析 API:不同的会话总和 'ga:eventLabel' 功能和没有它