By Dan Lorenz | Dec 2, 2013

Entity Framework 6.0 – Intercepting SQL produced by EF

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. (See our software development solutions)

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 Interceptor 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 MyInterceptor 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:

Dan is a highly skilled Solution Architect with deep expertise in .NET languages, Windows Forms/Services, ASP.NET / JavaScript, MonoDroid, and Win 8 (XAML) - among others.

You may also like

Healthcare and IT professionals discussing navigating compliance and threats in the modern cybersecurity landscape.

Is Your Healthcare IT Infrastructure Ready to Navigate Compliance and Threats?

Healthcare providers face mounting pressure to balance cybersecurity, compliance, and care delivery. Learn how hybrid infrastructure can help safeguard patient data, streamline operations, and meet evolving demands. Healthcare institutions are […]

Reimagine Your Hybrid and Remote Desktop Strategy with Cloud Endpoints and AVD

During this virtual discussion our panel of experts will discuss why companies need a new cloud endpoint management approach—one that enhances organizational remote hybrid and work capabilities. They will delve […]
Two automotive engineers conducting daily operations at a manufacturing plant.

Accelerate Transformation: Embrace Hybrid IT for a Smarter Supply Chain

Discover how hybrid IT strategies can unlock agility, efficiency, and resilience in your manufacturing operations. Did you know supply chain disruptions cost manufacturers billions each year? For the automotive industry, […]