Entity Framework Command Interception - Microsoft Entity Framework

What is Entity Framework Command Interception?

In Entity Framework 6.0, there is one more new attribute known as Interceptor or Interception. The interception code is build around the concept of interception interfaces. For instance, the IDbCommandInterceptor interface define methods that are called before EF makes a call to ExecuteNonQuery, ExecuteScalar, ExecuteReader, and related methods.

  • Entity Framework can truly glow by using interception. Using this approach you can capture a lot more information transiently without having to untidy your code.
  • To implement this, you need to create your own custom interceptor and register it accordingly.
  • Once a class that implements IDbCommandInterceptor interface has been created it can be registered with Entity Framework using the DbInterception class.
  • IDbCommandInterceptor interface has six methods and you need to implement all these methods. Following are the basic implementation of these methods.

Let’s take a look at the following code in which IDbCommandInterceptor interface is implemented.

Registering Interceptors

Once a class that implements one or more of the interception interfaces has been formed it can be registered with EF using the DbInterception class as exposed in the following code.


Interceptors can also be register at the app-domain level using the DbConfiguration code-based configuration as exposed in the following code.
You can also configure interceptor config file use the code −

You can also configure interceptor config file use the code −


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Entity Framework Topics