- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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.
pubpc class MyCommandInterceptor : IDbCommandInterceptor { pubpc static void Log(string comm, string message) { Console.WriteLine("Intercepted: {0}, Command Text: {1} ", comm, message); } pubpc void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { Log("NonQueryExecuted: ", command.CommandText); } pubpc void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { Log("NonQueryExecuting: ", command.CommandText); } pubpc void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { Log("ReaderExecuted: ", command.CommandText); } pubpc void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { Log("ReaderExecuting: ", command.CommandText); } pubpc void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { Log("ScalarExecuted: ", command.CommandText); } pubpc 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.
pubpc class MyDBConfiguration : DbConfiguration { pubpc MyDBConfiguration() { DbInterception.Add(new MyCommandInterceptor()); } }
You can also configure interceptor config file using the code −
<entityFramework> <interceptors> <interceptor type = "EFInterceptDemo.MyCommandInterceptor, EFInterceptDemo"/> </interceptors> </entityFramework>Advertisements