title: Best way to manage increment counter in C# .Net Core
body: <p>Given 2 tables <code>PaymentServices</code> and <code>PaymentTransaction</code>, the requirement is, for each payment services they can set counter start from what number. So in <code>PaymentServices</code> table I added column <code>Counter</code>. So user can set minimum counter to start. EG <code>5000</code>.</p>
  <p>So every transaction created it will count to <code>5001</code>, <code>5002</code> and etc then store the value in column <code>ReceiptNo</code> in table <code>PaymentTransaction</code></p>
  <p>So in my case, I can't use auto generate ID from database.</p>
  <p>Here is the code:</p>
  <pre class="lang-cs prettyprint-override"><code>var getPaymentServices = _context.PaymentServices.First(c=&gt;c.Id == SelectedPaymentServiceId);
  
  // Create new transaction and get current counter
  var addNewPayment = new PaymentTransaction
  { 
     PaymentServiceId = getPaymentServices.Id,
     Amount = AmountToPay,
     ReceiptNo = getPaymentServices.Counter,
  };
  
  getPaymentServices.Counter++;
  
  _context.Add(addNewPayment);
  
  await _context.SaveChangesAsync;
  </code></pre>
  <p>My QA found, some of the transaction got duplicate <code>ReceiptNo</code> after do load test (around 50000 request). Even no to much but, it will impact the customer billing system.</p>
  <p>What the best way to manage this?</p>