Can I access an object in IDbCommandInterceptor in Entity Framework

When implementing IDbCommandInterceptor, I can access the SQL command that was created for the command / query. Is it also possible to access the actual object of an object that is stored or retrieved during implemented methods?

Here is some fantasy code to demonstrate what I would like to do:

public class WidgetInterceptor : IDbCommandInterceptor { public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { var widget = interceptionContext.Entity as Widget; if(widget != null) { //Perform tasks on widget before it is saved... } } } 
+4
source share
1 answer

In the case of a request, there are no objects. An executed SQL command can return any number of rows that can lead to materialized objects or some kind of projection. You can access a DbContext or ObjectContext object, and through your ChangeTracker you can see the state of entities inside it, but as far as I know, there is no direct way to map a specific command that you execute with any specific objects. Depending on your context, the number, type and state of objects in ChangeTracker may be sufficient to accomplish what you need.

+1
source

All Articles