title: Entity Framework transaction
body: <p>I have a program which is running in duplicate on two servers.
  I need to select and update DB records in the program, so I need a table lock or transaction from EF, otherwise each copy of program can select and modify same records. While the first copy make DB changes the other one shouldn't run the same code section. </p>
  
  <p>I found TransactionScope in EF, but it isn't working properly, because while the first copy is running I can make many select and update on that tables in SQL Server MGM studio. </p>
  
  <p>I have a short code snippet, please validate it:</p>
  
  <pre><code>using (TransactionScope transaction = new TransactionScope())
  {
      //select some records which aren't locked by the other copy of the program
      //condition: Locked==null
      recipientsList = (from c in context.Recipients
                        where
                            c.SentToPlatform == false &amp;&amp; c.PopupID != null &amp;&amp;
                            c.Message.MessageStatus == 2 &amp;&amp; c.Locked == null
                        select c).Take(piecePerMinute).ToList();
  
      foreach (var recipient in recipientsList)
      {
          //i need make some changes on the record, prevent it from the other copy of program
          //I need to change locked column to true
          recipient.Locked = true;
          recipient.LockBy = ipAddress;
          Console.Write("I");
          Thread.Sleep(1000);
      }
  
      //close transaction
      try
      {
          context.SaveChanges();
          transaction.Complete();
      } catch (Exception ex )
      {
  
  
      }
  }
  </code></pre>