What is the minimal amount of code I can write to get a single callback from EF 4.1 that provides the following:

  • OnSQLExecuted(DbCommand cmd, DateTime start, double durationMS, string stacktrace)

At the moment we use a nasty hack that seems to be leaking performance, I am curious at how we can achieve this callback with a minimal amount of impact on the app.


We are able to wire this up in Mini Profiler by hacking around - intially we changed Database.DefaultConnectionFactory however mucking with the default factory means you can not have two profiling factories going at the same time. So we went the more aggressive route.


The technique commonly used is pretty straight forward, you implement: DbProviderFactory, IDbConnectionFactory, DbProviderServices, DbConnection, DbCommand and DbDataReader in such a way that they intercept the calls and profile.

So far, easy... however it gets messy when you try to wire this up:

   try
    {
        // ensure all the factories are loaded 
        DbProviderFactories.GetFactory("...");
    }
    catch (ArgumentException)
    {
    }

    Type type = typeof(DbProviderFactories);

    DataTable table;
    // SUPER UGLY - Can this be done in another way? 
    object setOrTable = (type.GetField("_configTable", BindingFlags.NonPublic | BindingFlags.Static) ??
                    type.GetField("_providerTable", BindingFlags.NonPublic | BindingFlags.Static)).GetValue(null);
    if (setOrTable is DataSet)
    {
        table = ((DataSet)setOrTable).Tables["DbProviderFactories"];
    }

    table = (DataTable)setOrTable;

    forea
Edit
Report