I have the following configuration:
builder.Register<EntityContext>().As(
c=> {
var entityCtx = new EntityContext();
//snip snip: some config stuff
return entityCtx;
}).As<MyDbContext>().InstancePerLifetimeScope();
EntityContext obviously inherits from MyDbContext which again inherits from DbContext.
In my repository’s constructor I’m usually using them as
...
public MyRepository(MyDbContext context) {...}
This ensures to have one ctx per http request which is what I want. But now I have the need that for a specific repository I want to have a different instance of the EntityContext than is normally being used.
How would you achieve that? The following came to my mind:
- Another config where I use
Register<EntityContext>()....As<DbContext>()...
Any other ideas?
I just found a proper solution that could work, namely to use the
OwnedInstancesof AutoFac if I understood correctly. For example:This scenario is especially useful if you’d like MyRepository to run in a different transaction, i.e. it needs to have a different
DbContextinstance than the other repositories.