
Posted on comments
An Introduction To Interception In EF Core
Entity Framework or EF Core is a lightweight, could be extended, and a cross-platform version of the commonly known Entity Framework data access technology.
EF Core is an object-relational mapper (O/RM) that allows .NET developers to work with a database that uses .NET objects. It removes the need for much of the data-access code which developers usually require to write codes. EF Core supports several database engines.
Presently, Entity Framework Core yet does not have all of the attributes provided by pre-Core versions. One of the features is an ability to intercept questions, something which was offered by the IDbCommandInterceptor, IDbCommandTreeInterceptor interfaces.
The EF Core 2.0 has an interception at the SQL level, and you can only get to this in a rather complicated way, via the DiagnosticSource mechanism. Here we shall discuss on how to create a class with two methods, as shown below:
Public class CommandListener
{
[DiagnosticName(“Microsoft.EntityFrameworkCore.Database.Command.CommandExecuting”)]
public void OnCommandExecuting(DbCommand command, DbCommandMethod executeMethod, Guid commandId, Guid connectionId, bool async, DateTimeOffset startTime)
{
}
[DiagnosticName (“Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted”)]
public void OnCommandExecuted(object result, bool async)
{
}
}
You will require the Microsoft.Extensions.DiagnosticAdapter NuGet package to assemble this.
The methods can be hooked to EF Core as shown below:
var listener = ctx.GetService<DiagnosticSource>();
(listener as DiagnosticListener).SubscribeWithAdapter(new CommandListener());
Here, ctx is a DbContext, and it is being tried to access its registered DiagnosticSource service, which is a DiagnosticListener. You need to add a subscriber, and that’s it!
From the OnCommandExecuted method you can summon these methods whatever you choose, as long you keep the [DiagnosticName] features, the result parameter would contain either a scalar, a number or a RelationalDataReader object a sync, and would tell you exactly what version of the method was implemented.
OnCommandExecuting is quite amazing because there you can get an access to the DbCommand which is on how to implement the SQL command and can edit its SQL or parameters.
There are other mechanisms of interception as well, like the IQuerySqlGenerator and IQueryCompiler, to be covered later.
With this, we conclude. Keep coding!
If you want to enhance yourself in basics and concepts with Dot Net Course and improve yourself through Dot NET training program; our institute would be of great help and support.
We give a well-structured program for the best Dot Net Course. Among several good institutes of dot net training and placement in Pune, CRBtech has created a niche for itself.
Code source: Development With A Dot
Enquire Now
Student Speaks

Celoxis Technologies Pvt. Ltd.

ZS Associates Pvt. Ltd.

Symphony Fintech Solutions Pvt. Ltd.

Saama Technologies Pvt. Ltd.

Network Components Pvt. Ltd.

Symphony Fintech Solutions Pvt. Ltd.

Jade Global Software Pvt. Ltd.

Jade Global Software Pvt. Ltd.

Accenture

Fujitsu Consulting India Pvt. Ltd.

Ross Process Equipment Pvt. Ltd.

Fujitsu Consulting India Pvt. Ltd.

Fujitsu Consulting India Pvt. Ltd.

Syneos Health Pvt. Ltd.

Syneos Health Pvt. Ltd.

Saba Software Pvt. Ltd.

Syneos Health Pvt. Ltd.

Fujitsu Consulting India Pvt. Ltd.

DressRand (Payroll CADtech) Pvt. Ltd.

C. C. Engineers Pvt. Ltd.

C. C. Engineers Pvt. Ltd.

Arora Technologies Pvt. Ltd.

Syneos Health

Syneos Health Pvt. Ltd.

Fujitsu Consulting India Pvt. Ltd.

Aquatech System Pvt. Ltd.

LIEBHERR CMCtec

Macart Equipments Pvt. Ltd.

Bobst India Pvt. Ltd.

Rendered Idea Softgame Pvt. Ltd.

Bobst India Pvt. Ltd.

Ubitech Systems Pvt. Ltd.

Fujitsu Consulting India Pvt. Ltd.

Design Tech Systems Pvt. Ltd.

ZS Associates Pvt. Ltd.

Tetra Pak India

Infosys

Zerebral IT Solutions Pvt. Ltd.

Inspeero Technologies Pvt. Ltd.

Extentia Information Technology Pvt. Ltd.

Centurysoft Pvt. Ltd.

ZS Associates Pvt. Ltd.

ZS Associates Pvt. Ltd.

Adsparx Pvt. Ltd.

SRKay Consulting Group

SLABS Engineering Pvt. Ltd.

Pubmatic Software Pvt. Ltd.

Media Agility Pvt. Ltd.

SLABS Engineering Pvt. Ltd.

SLABS Engineering Pvt. Ltd.

Cleantech Systems Pvt. Ltd.

Sears IT & Management Services Pvt. Ltd.

Vishay Components India Pvt. Ltd.

Extentia Information Technology Pvt. Ltd.

Gartech Equipment Pvt. Ltd.

Extentia Information Technology Pvt. Ltd.

Indovance Solutions Pvt. Ltd.

Black & Veatch Pvt. Ltd.

KPIT Technologies Pvt. Ltd.

Black & Veatch Pvt. Ltd.

IQVIA

GARTECH EQUIPMENT PVT. LTD

Microverse Automation Pvt. Ltd

GARTECH Equipments Pvt. Ltd. Pune

Manav Gases Pvt. Ltd

Hi-Physix Laboratory Pvt. Ltd.

Gartech Equipments Pvt. Ltd., Pune

THUSE ELEKTRONICS PVT. LTD

PAYPER BAGGING SYSTEMS PVT. LTD

Thuse Elecktronics Pvt. Ltd

Saama Technologies Pvt. Ltd

Stockholding DMS Ltd

Saama Technologies Pvt. Ltd

Vyom Labs

Saama Technologies Pvt. Ltd

HunkJet Technocraft Pvt Ltd

VANtage Systech Pvt. Ltd

Stockholding DMS Ltd

Twenty Twenty Interior Design Software Pvt. Ltd.

Twenty Twenty Interior Design Software Pvt. Ltd.

Twenty Twenty Interior Design Software Pvt. Ltd.

BISIL, Pune

FIGmd India Pvt. Ltd.

Eternus Solutions

Nelito Systems Ltd

Hoerbiger India Pvt. Ltd.

BISIL, Pune

Swan Aluminium Pvt Ltd.

Hyva India Pvt. Ltd.

Audi Germany

Twenty Twenty Interior Design Software Pvt.Ltd.

Eternus Solutions Pvt. Ltd.

CloudAction Tecchnology Services Pvt. Ltd.

Eternus Solutions Pvt. Ltd.

Eternus Solutions Pvt. Ltd.

Capegemini

eClinicalWorks India Private Limited

Sparken IT solution

ETLhive

Swaroop Techno Components

Covenant Solutions

FIGmd India Pvt. Ltd.

FIGmd India Pvt. Ltd.

Appotex

Fidel Softech Pvt. Ltd., Pune.

Stock Holding DMS Ltd., Mumbai

Eternus Solution, Pune

Fidel Softech Pvt. Ltd. Pune.

Quick Heal

CenturySoft Pvt. Ltd. Pune

Syntel