c# - How to use SqlBulkCopy and track Bulk Inserts with Glimpse -
in our app use sqlbulkcopy
class facilitate bulk loading of our database. recently, i've tried add glimpse our solution code fails invalid cast:
system.invalidcastexception: unable cast object of type 'glimpse.ado.alternatetype.glimpsedbconnection' type 'system.data.sqlclient.sqlconnection'
this because glimpse ado using wrapped sqlconnection
make magic possible. unfortunately, sqlbulkcopy
requires sqlconnection
need cast dbconnection
.
is there no out-of-the-box way profile bulk insertions? work-around came across far is:
(sqlconnection)((glimpsedbconnection)dbconnection).innerconnection
it's ugly since requires referencing glimpsedbconnection explicitly , requires adding custom time-line events tracing. there no glimpse add-on solves this?
i'm using approach (with entityframework's dbcontext
) , seems work:
dbcontext db = /* ... */; var glimpsedbconnection = db.database.connection glimpsedbconnection; var sqlconnection = glimpsedbconnection != null ? (sqlconnection)glimpsedbconnection.innerconnection : (sqlconnection)db.database.connection; var glimpsedbtransaction = db.database.currenttransaction.underlyingtransaction glimpsedbtransaction; var sqltransaction = glimpsedbtransaction != null ? (sqltransaction)glimpsedbtransaction.innertransaction : (sqltransaction)db.database.currenttransaction.underlyingtransaction; using (var bulkcopy = new sqlbulkcopy(sqlconnection, sqlbulkcopyoptions.default, sqltransaction)) { var datatable = /* ... */; bulkcopy.batchsize = 2000; bulkcopy.bulkcopytimeout = timespan.fromminutes(5).seconds; bulkcopy.destinationtablename = /* ... */; bulkcopy.writetoserver(datatable); }
Comments
Post a Comment