-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve experience for creating multiple instances of a DbContext in the same request #4441
Comments
👍 would be very nice ! |
@divega I don't remember discussing anything like this. I think it could work. |
BTW we already have |
Need to decide if this "factory" should be IDisposable and remember instances it created to flow the call to |
Whenever I've tackled exactly this question before I've always gone with:
Also, I've found the IDbContextFactory interface already provided is good for this purpose. I believe previous EF versions had some odd assembly scanning logic so that if EF found an IDBContextFactory in the same assembly as your DbContext, EF internals would use that to instantiate DbContexts.- Worth keeping in mind if your intended factory was for the layer above EF. |
@brendan-ssw totally agreed. If it handles the lifetime of the DbContext it is not just a factory anymore. That is why I started wrapping the word "factory" in quotes 😄 I will update the original description to account for this.
We still have scanning logic in our design time tools, e.g. see I can't recall at the moment why this is needed but @bricelam for sure will. |
|
@bricelam So, is there any way to configure the service container when invoking the dnx ef migration commands? Right now my EFCore test project has a DbContext with an empty constructor just so I can run migrations. |
Think of |
Just to repeat @bricelam's previous comment, we only use IDbContextFactory when we cannot figure out other ways to create the service container. We have conventions that look for |
(hit "Comment" too soon) command parameters you can use to tell EF Migrations where to find the startup project at design-time so that we can create the same service container configuration you would have at runtime. |
Per #4668 we removed magic injection for parameter-less constructors. The most intuitive way of creating non-scoped context instances (i.e. using the Given that, I suspect there may not be a big future for context factories at runtime. I still think that I propose we close this. Thoughts? |
@divega Agree it can be closed. |
👍 |
While responding to aspnet/DependencyInjection#352, I had what I think is a new idea (in the context of EF) that could help improve this experience. Here is a summary:
Add to the framework a new "factory" class, e.g. let's for now call it
DbContextFactory<TContext>
class. Its shape would look somewhat like this:The
AddDbContext()
method would register this in DI.Users that need a single context per request scope would still resolve the TContext directly in constructors and users that need to create multiple instances could resolve the factory and then create the individual instances manually using the
Create()
method.A few details/decision points:
DbContextActivator
mechanism but would be mostly sugar built on top of it (we still need a static place to stash the IServiceProvider).Create<TContext>()
method generic, plus change where we pass theDbContextOptions
, add aparams object[]
argument toCreate()
, etc.AddDbContext()
to always go through an instance of this class or just leave it as it is now.IDbContextFactory<T>
type that this class could implement and that would become the actual service interface but it is currently used for different purposes. We need to understand if there is synergy in having the same type for all purposes of if it makes sense to look elsewhere.IDbContextFactory<T>
for the creation.@ajcvickers I don't remember if we ever discussed doing something like this. Do you?
Another improvement in this area would be the ability to specify transient scoping when calling AddDbContext.
The text was updated successfully, but these errors were encountered: