-
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
Add an interceptor to an existing DbContext instance #20273
Comments
Putting this on the backlog as something that would be useful. However, traditional events may be more appropriate--see #626. |
So this means that there is no way to do it in the 3.1 version right ? |
@Giorgi Correct. |
There is also no way to get the command text and parameters from IQueryable<> neither. |
Yes, looks like I will have to wait until EF Core 5 |
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 |
@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. |
@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 |
You can do whatever you want in your implementation of |
It seems I grossly overestimated what was involved in creating a |
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 existingDbContext
instance?This can be useful for third party library developers which want to add interception capability to the existing
DbContext
instance.The text was updated successfully, but these errors were encountered: