Graeme Hill's Dev Blog

EFTracingConnection and CreateDbCommand

Star date: 2013.034

I've been using the EFTracingConnection class that comes as a part of the Entity Framework Provider Wrappers project. It works wonderfully well for tracking the SQL queries that get executed with the Entity Framework DbContext but I ran into a snag when I combined it with the EntityFramework.Extended library for batch operations. I started getting a NotSupportedException whenever I tried to execute a batch operation with the tracing connection enabled. It turns out the EntityFramework.Extended internally uses DbConnection.CreateDbCommand() to run its SQL. However, the EFTracingConnection implementation does not implement that method since it acts as a wrapper for DbConnection even though it also inherits from it (ie: EFTracingConnection has a DbConnection even though it also is a DbConnection). The fix is to make a subclass of EfTracingConnection and implement the method by invoking it on the wrapped object:

public class MyTracingConnection : EFTracingConnection
{
    protected override System.Data.Common.DbCommand CreateDbCommand()
    {
        return this.WrappedConnection.CreateCommand();
    }
}

So far this seems to work like a charm.

comments powered by Disqus