title: EF6: How to generate a unique number automatically based on entity Id
body: <p>Consider an entity like this:</p>
  
  <pre><code> public class Document
   {
  
      public int Id { get; set; }
  
      public long code { get; set; }
  
      // Rest of Props
  
   }
  </code></pre>
  
  <p>What I need is to generate a unique long code, and I prefer to generate it based on Id.</p>
  
  <p>One simple classic but unsafe solution is to get the last Id and increase it by one and use, but I'm looking for a better solution, such as computed column, or any other way to assign code generation to database.</p>
  
  <ul>
  <li>I cannot access to Id if I try define it as a compute column.</li>
  <li>Marking it as Identity is not possible, because Id is already an
  Identity column.</li>
  <li>Creating a method to Get the last Id is not safe and clean.</li>
  </ul>
  
  <p>By now I'm trying to do former classic solution with the help of transactions to make it safe.</p>
  
  <p>Any better suggestion?</p>