Interception in Entity Framework Core

Ashish Patel
.NET Hub
Published in
2 min readOct 5, 2019

--

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…

--

--

Ashish Patel
.NET Hub

Cloud Architect • 4x AWS Certified • 6x Azure Certified • 1x Kubernetes Certified • MCP • .NET • Terraform • DevOps • Blogger [https://bit.ly/iamashishpatel]