Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SqlBulkCopy to show up as sql in MiniProfiler

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);
}
like image 236
George Duckett Avatar asked Oct 22 '22 01:10

George Duckett


1 Answers

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
}
like image 164
Yaakov Ellis Avatar answered Oct 31 '22 12:10

Yaakov Ellis