title: How to prevent multiple users to update a record at the same time using LINQ
body: <p>I have below LINQ to update a record and in some cases it may be <code>IEnumerable</code> too.</p>
  <pre><code>public async Task&lt;IActionResult&gt; MyMethod(Int ID, decimal confirmQty)
  {               
      using (var tran = _context.Database.BeginTransaction())
      {
          try
          {
              // Need to Lock here whether single record or multiple records
              var invReduce = _context.Inventorys.Where(w=&gt;w.id == ID).FirstOrDefault();
              invReduce.availQty -= confirmQty;
  
              Thread.Sleep(60000); // One Min
              await _context.SaveChangesAsync();
              tran.Commit();
  
              // Need to Un-Lock here whether single record or multiple records
          }
          catch (Exception ex)
          {
              tran.Rollback();
          }
      }
  
      return Ok();
  }
  </code></pre>
  <p>Here, 1st user can query the data and should lock it to prevent 2nd user to querying same data. On 1st user's process completes, 2nd user query should automatically run.</p>
  <p><strong>Update</strong>: for example, for id:1, qty is 1000, first user requests to reduce qty by 100, also 2nd user sends requests for 100 to reduce at the same time and before 1st user's <code>SaveChanges()</code> takes effect. Final reduce qty should be 1000 - 100 - 100 = 800.</p>
  <p>So, till the time 1st user's operation completes, 2nd user's query should be in the queue.</p>
  <p>I use ASP.NET Core 2.2 code-first, PostgreSQL, no stored procedures.</p>
  <p>How do I lock rows here?</p>