Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting LINQ Inserts Before submitChanges and using BulkInsert Instead
    primarykey
    data
    text
    <p>I've got a bunch of code that is setup to add 10k to 20k inserts to a LINQ DB in addition to a few updates/deletes. All of these changes are committed through a db.SubmitChanges() call. The problem is it's <strong>too slow :(</strong> The performance has been thoroughly tested in this great post on StackOverflow:</p> <p><a href="https://stackoverflow.com/questions/3750999/very-slow-insert-process-using-linq-to-sql">Very slow insert process using Linq to Sql</a></p> <p>The issue is that I do not want to rewrite all of the code that prepares my DB object. I've attempted to psuedo code what I'd like to do below.</p> <p><strong>Disclaimer: This is not real code! It will not compile.... not even close :)</strong></p> <p>OLD WAY:</p> <pre><code>SchedDB.Data.SchedulerDBDataContext db = new SchedDB.Data.SchedulerDBDataContext(); //Do a bunch of stuff to populate "db" db.SubmitChanges() </code></pre> <p>NEW WAY:</p> <pre><code>SchedDB.Data.SchedulerDBDataContext db = new SchedDB.Data.SchedulerDBDataContext(); //Do a bunch of stuff to populate "db" //NEW: Detect all of the inserts in the "db" object. Remove those inserts and generate code to insert them with a batch dataadapter. For example: // //DataTable dtProducts = new DataTable() //dtProducts.Columns.Add(ProductID) //yada yada all of the columns here // //DataTable dtCustomers = new DataTable() //dtCustomers.Columns.Add(CustomerID) //yada yada all of the columns here //foreach (insertItem in db.Inserts) //this is pseudo code, I need help here //{ // if (insertItem.destinationTable == "Products") // { // DataRow dr = dtProducts.NewRow(); // dr["ProductID"] = insertItem.ProductID; //This line of code probably isn't even close to being right... I can't imagine the "insertItem" holding all of the columns no matter what the destinationTable is // } // if (insertItem.destinationTable == "Customers") // { // //similar code, all customer columns, yada yada // } // IMPORTANT: remove the item from the LINQ db so it doesn't insert it anymore // // Make a table adapter for each datatable // Set the .BatchSize parameter //Commit each table adapter. // db.SubmitChanges() //If there are any updates or deletes they are still in here and still need to happen. </code></pre> <p>If there are any errors in the mass updates then it would be nice to know that so I can roll back the LINQ db and potentially run some other code to clear the dataapapters inserts. (I can handle this, all of my inserts have an extra column that is an int that is unique to the batch insert.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload