-
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 IDbCommandInterceptor or similar to allow injecting custom query execution service #15066
Comments
"injecting session-wide information to support Row-Level-Security" is a critical feature preventing us from moving to EF Core. Currently, in EF6 we use DbCommandInterceptor and DbConnectionInterceptor to inject SQL to handle setting session context for SQL Server Row Level Security. Is there an existing way to do this in EF Core? |
Fixes #15066 The general idea is the same as `IDbCommandInterceptor` in EF6. Specifically: * Command interceptors can be used to view, change, or suppress execution of the `DbCommand` and to modify the result before it is returned to EF. `DbCommandInterceptor` is provided as an abstract base class. * Example of registration: ```C# builder.UseSqlServer( myConnectionString, b => b.CommandInterceptor(myInterceptor); ``` * Multiple interceptors can be composed using the `CompositeDbCommandInterceptor`. * Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptor is run last.
Fixes #15066 The general idea is the same as `IDbCommandInterceptor` in EF6. Specifically: * Command interceptors can be used to view, change, or suppress execution of the `DbCommand` and to modify the result before it is returned to EF. `DbCommandInterceptor` is provided as an abstract base class. * Example of registration: ```C# builder.UseSqlServer( myConnectionString, b => b.CommandInterceptor(myInterceptor); ``` * Multiple interceptors can be composed using the `CompositeDbCommandInterceptor`. * Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptor is run last.
@ajcvickers Partially. Looks like when implementing IDbCommandInterceptor we don't have a way to access original DbContext or service provider (and then get DbContext scoped service that we could use for command execution). Am I missing something?
|
@kosinsky @ajcvickers Injecting the DbContext in the interceptor's constructor would probably work, right? Or are only Singleton interceptors supported? |
Fixes #15066 The general idea is the same as `IDbCommandInterceptor` in EF6. Specifically: * Command interceptors can be used to view, change, or suppress execution of the `DbCommand` and to modify the result before it is returned to EF. `DbCommandInterceptor` is provided as an abstract base class. * Example of registration: ```C# builder.UseSqlServer( myConnectionString, b => b.CommandInterceptor(myInterceptor); ``` * Multiple interceptors can be composed using the `CompositeDbCommandInterceptor`. * Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptor is run last.
Fixes #15066 The general idea is the same as `IDbCommandInterceptor` in EF6. Specifically: * Command interceptors can be used to view, change, or suppress execution of the `DbCommand` and to modify the result before it is returned to EF. `DbCommandInterceptor` is provided as an abstract base class. * Example of registration: ```C# builder.UseSqlServer( myConnectionString, b => b.CommandInterceptor(myInterceptor); ``` * Multiple interceptors can be composed using the `CompositeDbCommandInterceptor`. * Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptor is run last.
Fixes #15066 The general idea is the same as `IDbCommandInterceptor` in EF6. Specifically: * Command interceptors can be used to view, change, or suppress execution of the `DbCommand` and to modify the result before it is returned to EF. `DbCommandInterceptor` is provided as an abstract base class. * Example of registration: ```C# builder.UseSqlServer( myConnectionString, b => b.CommandInterceptor(myInterceptor); ``` * Multiple interceptors can be composed using the `CompositeDbCommandInterceptor`. * Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptor is run last.
@ajcvickers Great! Just curious, but is there any reason the DbContext couldn't by injected in the interceptor via its constructor? Or would that give a circular reference somehow? Or is there perhaps another benefit I'm missing? |
@nphmuller Interceptors are currently resolved only from the internal service provider. So having one depend on |
@ajcvickers I didn't think about the non DI scenario, since DI seems so much like the normal route in ASP.NET Core. Thanks for the clear explanation! I learned about |
Sometime application needs more control on executing build SQL statement in the database to add custom retry logic, circuit breakers, CPU usage logging and throttling, injecting session-wide information to support Row-Level-Security etc. That requires injecting custom code that will get SQL Command from the EF Core (or just text + list of parameters), executing it and returning IDataReader back to the EF Core.
EF6 supported that approach by deriving from DbCommandInterceptor and overriding ReaderExecuting and ScalarExecuting.
This issue is a detailed case for #626
The text was updated successfully, but these errors were encountered: