Getting SqlBulkCopy to show up as sql in MiniProfiler

c# mvc-mini-profiler sqlbulkcopy

Question

I'm using MiniProfiler to profile my sql commands.

One issue I'm dealing with now is repeated INSERT statements generated by linq.

I've converted them into a SqlBulkCopy command, however now it doesn't appear to record it in the sql view in MiniProfiler.

Would there even be an associated command string for a SqlBulkCopy?

Is it possible to get the bulk copy to appear in the list of sql commands?

Can I at least make it counted in the % sql bit?


I'm aware I could use MiniProfiler.Current.Step("Doing Bulk Copy") but that wouldn't count as SQL, and wouldn't show in the listing with any detail.


Current code below:

public static void BulkInsertAll<T>(this DataContext dc, IEnumerable<T> entities)
{
    var conn = (dc.Connection as ProfiledDbConnection).InnerConnection as SqlConnection;
    conn.Open();

    Type t = typeof(T);

    var tableAttribute = (TableAttribute)t.GetCustomAttributes(
        typeof(TableAttribute), false).Single();
    var bulkCopy = new SqlBulkCopy(conn)
    {
        DestinationTableName = tableAttribute.Name
    };

    //....

    bulkCopy.WriteToServer(table);
}

Accepted Answer

You should be able to use CustomTimings to profile these. These are included in the new v3 version that is now available on nuget.

You can see some example usages of CustomTiming in the sample project where this is used to record http and redis events.

An example of how you could use it with SqlBulkCopy:

string sql = GetBulkCopySql(); // what should show up for the SqlBulkCopy event?
using (MiniProfiler.Current.CustomTiming("SqlBulkCopy", sql)) 
{
  RunSqlBulkCopy(); // run the actual SqlBulkCopy operation
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why