A community branch of the Entity Framework Tracing and Caching Provider Wrappers Sample Code.
- Install the
EF Tracing Provider NuGet Package.
- Derive a new context class from your existing context class (e.g., create a new
NorthwindContext, derived from the existing NorthwindEntities).
- Invoke the base constructor, passing the result of EFTracingProviderUtils.CreateTracedEntityConnection("NorthwindEntities") (replacing "NorthwindEntities" with the name of your connection string).
- Invoke EnableTracing in your constructor.
- Enable tracing in your app.config/web.config; the name of the TraceSource is "EntityFramework." prepended to your entity container name (so in this example it would be "EntityFramework.NorthwindEntities").
- In the rest of your code, use the derived context class (NorthwindContext) instead of the edmx-generated context (NorthwindEntities).
Example for step 2:
public class NorthwindContext : NorthwindEntities
Example for step 3:
<?xml version="1.0" encoding="utf-8"?>
<source name="EntityFramework.NorthwindEntities" switchValue="All" />
The sample configuration above will send SQL queries/updates (and their results) to the Output window when running under the debugger. They may also be observed using
Systems Internals' DbgView
when not running in the debugger. When deployed,
may be set to "Off" to disable tracing until it is needed. For an introduction to the .NET tracing system, see
Simple and Easy Tracing in .NET
Directly executing store commands using methods such as
is not supported. You may, however, create a
from the database connection using code such as this:
Original Sample Code
The original sample code
a blog post
that goes into some detail on how it all works. This community branch is almost the same as the original sample code.
The original EF Provider Wrappers sample code contained both a tracing provider and a caching provider. This project currently only includes the tracing provider. Community volunteers are welcome to port the caching provider!
The tracing provider has been changed to tie into the existing .NET tracing and instrumentation APIs rather than using its own file/console logging system.
A number of utility methods have also been added.