Back to List

Entity Framework 6.0 - Intercepting SQL produced by EF

Dan Lorenz Dan Lorenz  |  
Dec 02, 2013
 

Entity Framework 6.0 was recently released.  One of the new features added is called Interception.  You can now add “hooks” into each Context to catch events to log or even change values.  Setting up these Interceptors is very simple.

First, you need to create the Inteceptor class.  There are two interfaces that you can use: IDbCommandTreeInterceptor and IDbCommandInterceptor.  IDbCommandTreeInterceptor allows you to intercept the tree model that was created.  Currently, you can only modify the SELECT queries.  If EF creates a query that does not perform well, you can use this interceptor to modify the tree.  However, this will get cached, so the event will only fire the first time.  IDbCommandInterceptor allows you to manipulate the DbCommand object before and after Entity Framework makes the call to the database.  You have full access to everything on that command, including CommandText and Parameters.  You can log the SQL and the parameters.  You can modify the CommandText and/or the Parameters however you want.  Need to add SQL Server specific calls in the SQL being sent?  No problem!

We will use IDbCommandInterceptor as an example since it will most likely be the most used.  To create one, just create a new class and have it inherit from IDbCommandInterceptor.  There are six methods you must implement.  Here is what a basic skeleton would look like:

If you add code to the Executing calls, you can manipulate the SQL before it is being sent to the database.  If you add code to the Executed calls, you can manipulate the results that were returned by the database.

Finally, you need to create a static constructor for your context if you haven’t already.  Then you add one line of code that references the Inteceptor class you created.  From the previous example, the code would look like this:

That’s it!  Now whenever Entity Framework is about to make a call to the database, the methods in MyInteceptor will fire.  You can even put break points and see all the data being passed through.

If you need to pull out the current Context for some reason, you can do it via this code:

Entity Framework

 

Love our Blogs?

Sign up to get notified of new Skyline posts.

 


Related Content


Blog Article
How to Add Electronic and Digital Signatures to a Universal Application (UWP) with iText
Paul MadaryPaul Madary  |  
Aug 14, 2018
When paying for gas at the pump, checking out at Walmart, or electronically signing a contract to purchase real estate, have you ever thought about what technically goes into that electronic signature?  If not, then this is your lucky day! I recently had to implement this functionality in a...
Blog Article
Updated Mile of Music App Provides an Even Better User Experience
John PtacekJohn Ptacek  |  
Jul 26, 2018
As we get to the end of July at Skyline Technologies, our organization starts to get excited. We know that the Mile of Music festival is just around the corner. With over 70,000 people coming to Appleton, Wisconsin, for four days of original music, it is quite an adventure. Given one of the main...
Blog Article
WinForms Fluid Design - A Cautionary Tale
Jeff LucasJeff Lucas  |  
May 01, 2018
As a developer, you can find yourself digging into all sorts of code, even the occasional WinForms application.  WinForms come with a lot of handy features that can make fluid design quite simple, allowing your forms to be resized by the user but still hold the basic shape you desire without...
Blog Article
Using SignalR Base Classes in Angular
Eric DitterEric Ditter  |  
Mar 06, 2018
On one of my previous projects I used SignalR extensively in an Angular 1.6 application, and I was using the angular-signalr-hub library to integrate it into the application. It worked very well, but I am moving to the next version of Angular so I wanted to find a way to do it without having to...
Blog Article
How to Use .NET Core CLI to Create a Multi-Project Solution
Ben BuhrBen Buhr  |  
Feb 20, 2018
.NET Core has been around for a while (over three years if you count the release candidates). Over that time, the framework and tooling around it has evolved rapidly. I remember starting work on a new ASP.NET Core website back when the framework was in its first release candidate. At that time...