avatarAshish Patel

Summary

Entity Framework Core (EF Core) introduces an interception API that allows developers to inject custom logic during low-level database operations such as opening connections, committing transactions, or executing commands.

Abstract

The interception API in EF Core 3.x and later versions provides a mechanism for executing custom logic during various database operations. This feature enables developers to intercept and potentially alter or bypass these operations, which can be useful for tasks like logging, performance monitoring, or modifying queries and commands before they are executed. EF Core supports three types of interceptors: Command Interceptors for SQL statement manipulation, Transaction Interceptors for transaction-level operations, and Connection Interceptors for connection-level activities. Developers can create these interceptors by implementing specific EF Core interfaces and register them with the DbContext to automatically invoke the custom logic during the database operations.

Opinions

  • The interception API is seen as a powerful feature for developers working with EF Core, offering flexibility and control over database operations.
  • Interceptors are compared to similar features available in EF 6, suggesting a proven utility and familiarity for developers transitioning from earlier versions.
  • The ability to intercept operations before they occur and potentially bypass them is highlighted as a key advantage, allowing for alternate execution paths or results.
  • The article implies that interceptors can simplify the implementation of cross-cutting concerns like logging or security checks by centralizing such logic within the interceptors.
  • By mentioning the registration of interceptors with the DbContext, the article suggests that integration into existing EF Core applications can be straightforward.

Interception in Entity Framework Core

Entity Framework Core Interceptor: Interception of database operations.

Interception in Entity Framework Core

The new interception API in EF Core (3.x or later) allows providing custom logic to be invoked automatically whenever low-level database operations occur as part of the normal operation of EF Core. For example, when opening connections, committing transactions, or executing commands.

Similarly to the interception features that existed in EF 6, interceptors allow you to intercept operations before or after they happen. When you intercept them before they happen, you are allowed to by-pass execution and supply alternate results from the interception logic.

It enables writing simple logic that is invoked automatically by EF Core whenever, for example, a database connection is opened, a transaction is committed, or a query is executed. Interceptors usually allow you to intercept operations before or after they happen. When you intercept them before they happen, you are allowed to by-pass execution and supply alternate results from the interception logic.

There are three kinds of Interceptors available in EF Core:

  1. Command Interceptors — applied to SQL statements level
  2. Transaction Interceptors — applied to SQL transactions level
  3. Connection Interceptors —applied to SQL connections level

To manipulate connection, you can create an DbConnectionInterceptor:

To manipulate command text, you can create an DbCommandInterceptor:

Register interceptors with DbContext:

View more from .NET Hub

Happy Coding!!!

Aspnetcore
Entity Framework Core
Entity Framework
Interceptors
Csharp
Recommended from ReadMedium