我有以下配置:
builder.Register<EntityContext>().As(
c=> {
var entityCtx = new EntityContext();
//snip snip: some config stuff
return entityCtx;
}).As<MyDbContext>().InstancePerLifetimeScope();
EntityContext
显然继承自 MyDbContext
,而后者又继承自 DbContext
。
在我的存储库构造函数中,我通常使用它们作为:
...
public MyRepository(MyDbContext context) {...}
这会确保每个 HTTP 请求都有一个上下文(ctx),这正是我想要的。但现在我需要为特定存储库使用与通常使用的 EntityContext 不同的实例。
你该怎么做呢?以下是我的一些想法:
- 另一个配置,其中我使用
Register<EntityContext>()....As<DbContext>()...