填充使用新创建的 DbContext 后,依赖注入 DbContext 为空(EF7,ASP.NET 5,vnext)
Dependancy Injected DbContext is empty after populating DbContext created with new (EF7, ASP.NET 5, vnext)
我对 EF7 比较陌生,听说将 DbContext 依赖注入到 Controller 构造函数中是获取 DbContext 以在给定 Action 方法中使用的好方法。但是有很多情况是无法进行Dependency Injection的(比如普通访问Db类),必须使用using(VectorDbContext dbContext...)
模式
我遇到了 运行 一个问题,即向使用 using
模式创建的 DbContext 添加数据后,依赖注入的上下文无法访问该问题。 DbContext 是一个简单的 InMemory 数据库,用于测试 - 它不连接任何东西。
这是将实体添加到 DbContext 的代码,为了测试我在 Startup.cs:
中调用它
using (ExampleDbContext dbContext= new ExampleDbContext()) {
dbContext.Things.Add(
new Thing() {
Stuff= "something"
});
dbContext.SaveChanges();
}
控制器内的访问代码如下:
public class ExampleController : Controller {
public ExampleController(ExampleDbContext exampleDbContext) {
this.ExampleDbContext= exampleDbContext;
}
public ExampleDbContext ExampleDbContext { get; set; }
public async Task<IActionResult> ExampleAction() {
// new DbContext:
using(ExampleDbContext dbContext = new ExampleDbContext ()) {
var List1 = (await dbContext.Things
.AsNoTracking()
.ToListAsync());
}
// Injected DbContext:
var List2 = (await this.ExampleDbContext.Things
.AsNoTracking()
.ToListAsync());
}
}
单步执行时,List1 中有预期的一项,但 List2 始终为空!
我做错了什么?看起来 DbContexts 不知何故不同步,依赖注入如何创建 DbContext/where does it come from?
编辑:我刚刚做了一些额外的测试,并确认在使用 new
创建的 DbContext 中添加的任何实体仅在 new
中可见,并且在 Injected DbContext 中添加的实体仅在注入的 DbContext 中可见,让我相信它们正在连接到不同的后备数据库,但我无法确认。
我可能错了,但我的假设是,当您在代码中创建 DbContext
的新实例时,您使用的是无参数构造函数,该构造函数将基础连接字符串设置为某个默认值。但是,DI 注入 DbContext
可以使用另一个构造函数来解决,该构造函数具有显式传入的不同连接字符串。
这是一个显式指定构造函数参数的 Unity 配置示例:
<register type="DbContext, [assembly-name]" mapTo="DbContext, [assembly-name]">
<constructor>
<param name="nameOrConnectionString" value="Test"/>
</constructor>
</register>
所以我会检查您容器的配置。
我对 EF7 比较陌生,听说将 DbContext 依赖注入到 Controller 构造函数中是获取 DbContext 以在给定 Action 方法中使用的好方法。但是有很多情况是无法进行Dependency Injection的(比如普通访问Db类),必须使用using(VectorDbContext dbContext...)
模式
我遇到了 运行 一个问题,即向使用 using
模式创建的 DbContext 添加数据后,依赖注入的上下文无法访问该问题。 DbContext 是一个简单的 InMemory 数据库,用于测试 - 它不连接任何东西。
这是将实体添加到 DbContext 的代码,为了测试我在 Startup.cs:
中调用它using (ExampleDbContext dbContext= new ExampleDbContext()) {
dbContext.Things.Add(
new Thing() {
Stuff= "something"
});
dbContext.SaveChanges();
}
控制器内的访问代码如下:
public class ExampleController : Controller {
public ExampleController(ExampleDbContext exampleDbContext) {
this.ExampleDbContext= exampleDbContext;
}
public ExampleDbContext ExampleDbContext { get; set; }
public async Task<IActionResult> ExampleAction() {
// new DbContext:
using(ExampleDbContext dbContext = new ExampleDbContext ()) {
var List1 = (await dbContext.Things
.AsNoTracking()
.ToListAsync());
}
// Injected DbContext:
var List2 = (await this.ExampleDbContext.Things
.AsNoTracking()
.ToListAsync());
}
}
单步执行时,List1 中有预期的一项,但 List2 始终为空!
我做错了什么?看起来 DbContexts 不知何故不同步,依赖注入如何创建 DbContext/where does it come from?
编辑:我刚刚做了一些额外的测试,并确认在使用 new
创建的 DbContext 中添加的任何实体仅在 new
中可见,并且在 Injected DbContext 中添加的实体仅在注入的 DbContext 中可见,让我相信它们正在连接到不同的后备数据库,但我无法确认。
我可能错了,但我的假设是,当您在代码中创建 DbContext
的新实例时,您使用的是无参数构造函数,该构造函数将基础连接字符串设置为某个默认值。但是,DI 注入 DbContext
可以使用另一个构造函数来解决,该构造函数具有显式传入的不同连接字符串。
这是一个显式指定构造函数参数的 Unity 配置示例:
<register type="DbContext, [assembly-name]" mapTo="DbContext, [assembly-name]">
<constructor>
<param name="nameOrConnectionString" value="Test"/>
</constructor>
</register>
所以我会检查您容器的配置。