title: Multiple Insert and Update causing DeadLock c#
body: <p>I have two parts to my application which both does massive amount of insert and update respectively and because or poor managemenent, there's deadlock.</p>
  
  <p>I am using entity framework to do my insert and update.</p>
  
  <p>The following is my code for my TestSpool program. The purpose of this program is to insert x number of records with a given interval.</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Threading;
  using System.Transactions;
  namespace TestSpool
  {
      class Program
      {
          static void Main(string[] args)
          {
              using (var db = new TestEntities())
              {
                  decimal start = 700001;
                  while (true)
                  {
                      using (TransactionScope scope = new TransactionScope())
                      {
                          //Random ir = new Random();
                          //int i = ir.Next(1, 50);
                          var objs = db.BidItems.Where(m =&gt; m.BidItem_Close == false);
                          foreach (BidItem bi in objs)
                          {
                              for (int j = 0; j &lt;= 10; j++)
                              {
                                  Transaction t = new Transaction();
                                  t.Item_Id = bi.BidItemId;
                                  t.User_Id = "Ghost";
                                  t.Trans_Price = start;
                                  t.Trans_TimeStamp = DateTime.Now;
                                  start += 10;
                                  db.Transactions.AddObject(t);
  
                              }
                              Console.WriteLine("Test Spooled for item " + bi.BidItemId.ToString() + " of " + 11 + " bids");
                              db.SaveChanges();
                          }
  
                          scope.Complete();
                          Thread.Sleep(5000);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The second part of the program is the testserverclass, the serverclass supposed to processed a huge amount of transactions from testspool and determined the highest amount of the transaction and update to another table.</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Transactions;
  public class TestServerClass
  {
  
      public void Start()
      {
          try
          {
  
              using (var db = new TestServer.TestEntities())
              {
  
                  while (true)
                  {
                      using (TransactionScope scope = new TransactionScope())
                      {
                          var objsItems = db.BidItems.Where(m =&gt; m.BidItem_Close == false);
                          foreach (TestServer.BidItem bi in objsItems)
                          {
                              var trans = db.Transactions.Where(m =&gt; m.Trans_Proceesed == null &amp;&amp; m.Item_Id == bi.BidItemId).OrderBy(m =&gt; m.Trans_TimeStamp).Take(100);
  
                              if (trans.Count() &gt; 0)
                              {
                                  var tran = trans.OrderByDescending(m =&gt; m.Trans_Price).FirstOrDefault();
  
                                  // TestServer.BidItem bi = db.BidItems.FirstOrDefault(m =&gt; m.BidItemId == itemid);
                                  if (bi != null)
                                  {
                                      bi.BidMinBid_LastBid_TimeStamp = tran.Trans_TimeStamp;
                                      bi.BidMinBid_LastBidAmount = tran.Trans_Price;
                                      bi.BidMinBid_LastBidBy = tran.User_Id;
  
                                  }
                                  foreach (var t in trans)
                                  {
                                      t.Trans_Proceesed = "1";
                                      db.Transactions.ApplyCurrentValues(t);
                                  }
  
                                  db.BidItems.ApplyCurrentValues(bi);
                                  Console.WriteLine("Processed " + trans.Count() + " bids for Item " + bi.BidItemId);
                                  db.SaveChanges();
  
                              }
  
  
                          }
                          scope.Complete();
  
                      }
  
                  }
  
              }
  
  
          }
          catch (Exception e)
          {
              Start();
          }
      }
  
  }
  </code></pre>
  
  <p>However, as both application con-currently runs, it will go into deadlock pretty fast randomly either from the first test or server application. How do i optimised my code for both side to prevent deadlocks ? I am expecting huge amount of inserts from the testspool application.</p>