- Entity Framework Tutorial
- Entity Framework - Home
- Entity Framework - Overview
- Entity Framework - Architecture
- Entity F - Environment Setup
- Entity Framework - Database Setup
- Entity Framework - Data Model
- Entity Framework - DbContext
- Entity Framework - Types
- Entity Framework - Relationships
- Entity Framework - Lifecycle
- Entity F - Code First Approach
- Entity F - Model First Approach
- Entity F - Database First Approach
- Entity Framework - DEV Approaches
- Entity F - Database Operations
- Entity Framework - Concurrency
- Entity Framework - Transaction
- Entity Framework - Views
- Entity Framework - Index
- Entity F - Stored Procedures
- Entity F - Disconnected Entities
- Entity F - Table-Valued Function
- Entity Framework - Native SQL
- Entity Framework - Enum Support
- Entity F - Asynchronous Query
- Entity Framework - Persistence
- Entity F - Projection Queries
- Entity F - Command Logging
- Entity F - Command Interception
- Entity Framework - Spatial Data Type
- Entity Framework - Inheritance
- Entity Framework - Migration
- Entity Framework - Eager Loading
- Entity Framework - Lazy Loading
- Entity Framework - Explicit Loading
- Entity Framework - Validation
- Entity Framework - Track Changes
- Entity Framework - Colored Entities
- Entity F - Code First Approach
- Entity Framework - First Example
- Entity Framework - Data Annotations
- Entity Framework - Fluent API
- Entity Framework - Seed Database
- Entity F - Code First Migration
- Entity F - Multiple DbContext
- Entity F - Nested Entity Types
- Entity Framework Resources
- Entity Framework - Quick Guide
- Entity Framework - Useful Resources
- Entity Framework - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Entity Framework - Command Interception
In Entity Framework 6.0, there is another new feature known as Interceptor or Interception. The interception code is built around the concept of interception interfaces. For example, the IDbCommandInterceptor interface defines methods that are called before EF makes a call to ExecuteNonQuery, ExecuteScalar, ExecuteReader, and related methods.
Entity Framework can truly shine 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.
public class MyCommandInterceptor : IDbCommandInterceptor { public static void Log(string comm, string message) { Console.WriteLine("Intercepted: {0}, Command Text: {1} ", comm, message); } public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { Log("NonQueryExecuted: ", command.CommandText); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { Log("NonQueryExecuting: ", command.CommandText); } public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { Log("ReaderExecuted: ", command.CommandText); } public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { Log("ReaderExecuting: ", command.CommandText); } public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { Log("ScalarExecuted: ", command.CommandText); } public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { Log("ScalarExecuting: ", command.CommandText); } }
Registering Interceptors
Once a class that implements one or more of the interception interfaces has been created it can be registered with EF using the DbInterception class as shown in the following code.
DbInterception.Add(new MyCommandInterceptor());
Interceptors can also be registered at the app-domain level using the DbConfiguration code-based configuration as shown in the following code.
public class MyDBConfiguration : DbConfiguration { public MyDBConfiguration() { DbInterception.Add(new MyCommandInterceptor()); } }
You can also configure interceptor config file using the code −
<entityFramework> <interceptors> <interceptor type = "EFInterceptDemo.MyCommandInterceptor, EFInterceptDemo"/> </interceptors> </entityFramework>