Project Description
A community branch of the Entity Framework Tracing and Caching Provider Wrappers Sample Code.

QuickStart
  1. Install the EF Tracing Provider NuGet Package.
  2. Derive a new context class from your existing context class (e.g., create a new NorthwindContext, derived from the existing NorthwindEntities).
    1. Invoke the base constructor, passing the result of EFTracingProviderUtils.CreateTracedEntityConnection("NorthwindEntities") (replacing "NorthwindEntities" with the name of your connection string).
    2. Invoke EnableTracing in your constructor.
  3. 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").
  4. In the rest of your code, use the derived context class (NorthwindContext) instead of the edmx-generated context (NorthwindEntities).

Example for step 2:

using EFTracingProvider;
namespace WindowsFormsApplication1
{
    public class NorthwindContext : NorthwindEntities
    {
        public NorthwindContext()
            : base(EFTracingProviderUtils.CreateTracedEntityConnection("NorthwindEntities"))
        {
            this.EnableTracing();
        }
    }
}

Example for step 3:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="EntityFramework.NorthwindEntities" switchValue="All" />
    </sources>
  </system.diagnostics>
</configuration>

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, switchValue 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

Known Problems

Directly executing store commands using methods such as ObjectContext.ExecuteStoreCommand or ObjectContext.ExecuteStoreQuery is not supported. You may, however, create a DbCommand from the database connection using code such as this:
using EFProviderWrapperToolkit;
...
context.Connection.GetStoreConnection().CreateCommand()

Original Sample Code

The original sample code has 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.

Project State

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.

Last edited May 4, 2011 at 8:17 PM by StephenCleary, version 5