title: Asp.Net Web API preventing record duplication
body: <p>I am using C#.NET Web API for my iOS application but I have concerns about multiple requests arrive at the same time.</p>
  
  <p>Let's assume I try to prevent duplicating records while inserting a new record into Users table by:</p>
  
  <ol>
  <li>Check if <code>xxx@example.com</code> exists in the Users table.</li>
  <li>Insert if not exists.</li>
  <li>Return <code>OK</code>.</li>
  </ol>
  
  <p>Actually it's that simple unless web api runs async.</p>
  
  <p>What if related web api method gets two requests at the same time (with same e-mail request) and when the first request reaches step 2 (but not executed yet) and second request will get "not exists" response since step two for first request has not been executed yet. Then two e-mail address will be saved and I will have duplicated records.</p>
  
  <p>Using <code>lock</code> on static object seems will solve the problem but it will create performance issues.</p>
  
  <p>If I don't want DB to get rows duplicated, how can I overcome by that problem?</p>
  
  <pre><code>UPDATE:
  </code></pre>
  
  <p>I can't use unique constraint on e-mail column due to I already have it on Id column.</p>