Skip to content

Register a scoped DbContext automatically when using AddDbContextFactory #25164

Description

@davidfowl

Applications that need to inject the db context into background services (which are singletons) should be using the IDbContextFactory<TContext> instead of manually creating a scope and resolving the DbContext from it.

When I call services.AddDbContext<TContext> automagically add the supporting services to make injection of an IDbContextFactory work.

Activity

  1. ajcvickers commented on Jun 28, 2021

    @ajcvickers
    Contributor

    We don't currently do this because the context instances injected will be disposed by D.I., while the factory returns instances that need to be disposed explicitly. So, to help this not be a pit-of-failure, we require registration of the factory explicitly if you want to use it.

  2. vflame commented on Jun 30, 2021

    @vflame

    Is the following DI registration appropriate/correct?

    services.AddDbContextFactory<MyDbContext>((sp, options) =>
                {
                    options.UseNpgsql("CONNECTIONSTRING");
                }).AddScoped<MyDbContext>(p => p.GetRequiredService<IDbContextFactory<MyDbContext>>().CreateDbContext());

    Controllers get the standard ctor MyDbContext injection (http request scoped, no dispose) and background services/blazor components get the IDbContextFactory injection (custom scope, requires dispose).

    Is it safe to assume that, in the above registration, that scoped instances of MyDbContext will be dispose once the scope is disposed?

  3. added this to the 6.0.0 milestone on Jul 12, 2021
  4. ajcvickers commented on Aug 6, 2021

    @ajcvickers
    Contributor

    Results from design investigation:

    • AddDbContext registers DbContextOptions as scoped by default. This cannot be consumed from the default singleton factory. Changing the lifetime of DbContextOptions is a breaking change. This prevents us from adding the factory automatically, since it will not work correctly in the default/normal case.
    • AddDbContextFactory can automatically add a scoped DbContext registration. This will depend on the singleton options added with the factory, which will work. This is the change we will make.
    • Applications that explicitly register a DbContext as well as using AddDbContextFactory will not be impacted, since we always TryAdd in our methods.
    • Applications that are already calling AddDbContextFactory first, and then also AddDbContext, would have the registration made by AddDbContext ignored, since AddDbContext uses TryAdd. This would be breaking for these applications. Therefore, if AddDbContextFactory has been called, and then AddDbContext is also called, AddDbContext will replace the registration made by AddDbContextFactory. This could also be breaking in corner cases, but is the best we can do if we want to implement this at all.
  5. added a commit that references this issue on Aug 6, 2021
    a9bac1b
  6. roji commented on Aug 6, 2021

    @roji
    Member

    From #25440 (review):

    AddDbContext registers DbContextOptions as scoped by default. This cannot be consumed from the default singleton factory. Changing the lifetime of DbContextOptions is a breaking change. This prevents us from adding the factory automatically, since it will not work correctly in the default/normal case.

    Is there a downside to registering a factory as scoped in this case (in other words, always register the factory with the scope of the options)? This would ensure that the factory is consistently available in DI, without an extra gesture.

  7. davidfowl commented on Aug 6, 2021

    @davidfowl
    MemberAuthor

    It wouldn't be possible to inject it into a singleton then

  8. roji commented on Aug 6, 2021

    @roji
    Member

    @davidfowl conversation happening in #25440, will answer there.

  9. changed the title [-]Add IDbContextFactory<T> when calling AddDbContext[/-] [+]Register a scoped DbContext automatically when using AddDbContextFactory[/+] on Aug 11, 2021
  10. modified the milestones: 6.0.0, on Aug 12, 2021
  11. modified the milestones: , 6.0.0 on Nov 8, 2021
  12. removed their assignment
    on Sep 1, 2024
  13. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions