Skip to content

Add an interceptor to an existing DbContext instance #20273

Description

@Giorgi

I know that interceptors can be added when creating the model using DbContextOptionsBuilder.AddInterceptors method but is there a way to add an interceptor instance on an existing DbContext instance?

This can be useful for third party library developers which want to add interception capability to the existing DbContext instance.

Activity

  1. ajcvickers commented on Mar 15, 2020

    @ajcvickers
    Contributor

    Putting this on the backlog as something that would be useful. However, traditional events may be more appropriate--see #626.

  2. added this to the Backlog milestone on Mar 15, 2020
  3. Giorgi commented on Mar 15, 2020

    @Giorgi
    ContributorAuthor

    So this means that there is no way to do it in the 3.1 version right ?

  4. ajcvickers commented on Mar 15, 2020

    @ajcvickers
    Contributor

    @Giorgi Correct.

  5. Giorgi commented on Mar 15, 2020

    @Giorgi
    ContributorAuthor

    There is also no way to get the command text and parameters from IQueryable<> neither.

  6. ajcvickers commented on Mar 15, 2020

    @ajcvickers
    Contributor
  7. Giorgi commented on Mar 15, 2020

    @Giorgi
    ContributorAuthor

    Yes, looks like I will have to wait until EF Core 5

  8. changed the title [-]Add interceptor on an existing DbContext item.[/-] [+]Add an interceptor to an existing DbContext instance[/+] on Jan 14, 2022
  9. jamiewinder commented on Feb 27, 2023

    @jamiewinder

    Since this is quite an old issue, is there a way to do this currently? In my case, I'm hoping to be able to attach a connection interceptor to an existing DbContext (or through a IDbContextFactory if more appropriate) with an interceptor that performs tenancy isolation. Basically so I can implement a DbContext CreateTenantContext(Guid tenantId) factory method. As far as I can tell, my only option at the moment is to create a DbContext, force the connection to open (so it stays open), and then run a command on it.

  10. ajcvickers commented on Feb 28, 2023

    @ajcvickers
    Contributor

    @jamiewinder With a context factory, you control creating the instance of the context, so you can add whatever interceptors you want there. There isn't a way to add interceptors after the context has been initialized; this issue is tracking that.

  11. jamiewinder commented on Feb 28, 2023

    @jamiewinder

    @ajcvickers Thanks. Just to make sure I'm understanding correctly, I think I'm correct in saying that currently I can only control which interceptors are added when the factory is created rather than when I ask if for a context to be created? i.e. I can't do anything like this:

    private readonly IDbContextFactory<MyContext> _contextFactory;
    
    public MyContext CreateTenantContext(Guid tenantId)
    {
       var setTenancyInterceptor = new SetTenancyInterceptor(tenantId);
       return _contextFactory.CreateDbContextWithInterceptors(setTenancyInterceptor);
    }

    (obviously CreateDbContextWithInterceptors isn't a thing, just an example)

  12. ajcvickers commented on Feb 28, 2023

    @ajcvickers
    Contributor

    You can do whatever you want in your implementation of IDbContextFactory.

  13. jamiewinder commented on Feb 28, 2023

    @jamiewinder

    It seems I grossly overestimated what was involved in creating a IDbContextFactory! That's perfect, thanks for the pointers!

  14. 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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions