Skip to content

Make AddPooledDbContextFactory register DbContext in addition to the and factory #26528

Description

@araxemy

In #25164 it was suggested that EF Core implicitly registers a scoped DbContext as well when AddDbContextFactory is used, which I thought was a great idea, and it was done in RC1 I think.

But why not do the same thing for AddPooledDbContextFactory too?! I'm using AddPooledDbContextFactory in my project and I was surprised to see that the same thing isn't done in that method as well.

Activity

  1. changed the title [-]Register a scoped DbContext automatically when using `AddPooledDbContextFactory` as well[/-] [+]Register a scoped `DbContext` automatically when using `AddPooledDbContextFactory` as well[/+] on Nov 3, 2021
  2. self-assigned this
    on Nov 5, 2021
  3. added this to the 7.0.0 milestone on Nov 5, 2021
  4. changed the title [-]Register a scoped `DbContext` automatically when using `AddPooledDbContextFactory` as well[/-] [+]Register a scoped `DbContext` automatically when using `AddPooledDbContextFactory`[/+] on Nov 11, 2021
  5. msallin commented on Dec 28, 2021

    @msallin

    Same situation here. We use AddPooledDbContextFactory in all projects but we also use the EF HealthChecks, which expect the DbContext as ctor parameter. Hence, we also have to call AddDbContextPool<T>.

  6. modified the milestones: 7.0.0, Backlog on Jul 7, 2022
  7. aradalvand commented on Jul 18, 2022

    @aradalvand

    @ajcvickers Hi there, any reason as to why this was removed from 7.0.0? It seems like it would be relatively straightforward and simple to implement.
    Thank you.

  8. ajcvickers commented on Jul 18, 2022

    @ajcvickers
    Contributor

    @aradalvand We had to punt a number of smaller issues. While each in itself would not take a long time, that time quickly adds up. This one is also only sugar--it's easy to obtain the same functionality by registering trice--which makes it lower priority.

  9. 14 remaining items

  10. changed the title [-]Register both DbContext and factory in DI with a single call[/-] [+]Make AddPooledDbContextFactory register DbContext in addition to the and factory[/+] on Jan 16, 2025
  11. self-assigned this
    on Jan 16, 2025
  12. added this to the 10.0.0 milestone on Jan 16, 2025
  13. added a commit that references this issue on Jan 16, 2025
    3f22ec8
  14. added a commit that references this issue on Jan 17, 2025
    47e5d25
  15. susickypavel commented on May 9, 2025

    @susickypavel

    @roji Based on the labels I believe this was released as a part of .NET 10 preview 1?

    Until .NET 10 is released it is ok for lower versions to register the TContext with following code?

    services.AddPooledDbContextFactory<BlogContext>
    services.TryAddScoped(sp => sp.GetRequiredService<IDbContextFactory<BlogContext>>().CreateDbContext());
    

    Will be the BlogContext returned from the pool if request through DI this way? Thank you for clarification.

  16. roji commented on May 12, 2025

    @roji
    Member
  17. added theissue type on Jun 15, 2025
  18. JohnYoungers commented on Dec 18, 2025

    @JohnYoungers

    It might be a good idea to call this out as a notable change here: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-10.0/breaking-changes

    All of our applications register the DbContextFactory by default (even if they may not use it) opposed to DbContext: it looks like we had an app that injected a scoped level value into DbContext, which had been working fine with the pre-v10 setup, since they never injected DbContextFactory; it took a bit to dig into why that was happening.

    It wouldn't have been an issue had they originally marked the factory as being at the scope level, but it was a bit confusing as to why that had been working and suddenly stopped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions