Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Stop using var so much.</p> <hr> <p>This runs the query twice (observe in the sqlprofiler).</p> <pre><code>if (query.Count() &gt; 0) { foreach (var i in query) </code></pre> <p>Use this to avoid running the query multiple times.</p> <pre><code>List&lt;InventoryGoods&gt; rows = query.ToList(); </code></pre> <hr> <p>Your code has much repetition. Also, you're trying to do too little in the context in each Submit. You're controlling transaction scope for no reason.</p> <pre><code>foreach(InventoryGood i in rows) { InventoryGoodsEvent ige = new InventoryGoodsEvent() //this will attach ige to the object graph tracked by context // which is sufficient to insert ige when submitchanges is called. ige.InventoryGood = i; GoodsEvent ge = GetGoodsEvent(i); //all that conditional logic in there. //this will attach ge to the object graph tracked by context // which will both insert ge and update ige with ge's id when submitchanges is called. ige.GoodsEvent = ge; i.ActionOn = DateTime.Now; //to submit each row, uncomment this. //context.SubmitChanges(); } //to submit all rows at once, use this. context.SubmitChanges(); </code></pre> <p>If InventoryGoodEgvents doesn't have those relational properties, go into the designer and add associations to create them.</p> <p>Once you have such code, then you get to decide what's a good amount of change to do in one transaction. I like to insert ~100 records per transaction. If you use 1 record per transaction, there's a high overhead of creating each transaction. If you use 1,000,000 rows per transaction, there's the high overhead of a long running transaction.</p> <p>This stuff is hard to learn, but keep at it.</p> <p>One more thing: Bulk insert isn't going to work with that many-to-many table.</p>
 

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