title: Proper way to calculate order via entity framework / sql
body: <p>Is this a good way to set the transaction level to serializable? If I set a lower transaction level, I will get duplicate orders. What is better approach? I assume this method will be invoked frequently by different users.</p>
  <p>Here is my code</p>
  <pre><code>using (var tran = context.Database.BeginTransaction(IsolationLevel.Serializable))
  {
      try
      {
          var lastDocument = context.Documents.OrderByDescending(x =&gt; x.Id).FirstOrDefault();
          int order = 1;
          if (lastDocument != null)
          {
              order = lastDocument.Order + 1;
          }
          var document = new Document
          {
              CreatedDate = DateTimeOffset.UtcNow,
              Name = Guid.NewGuid().ToString(),
              Order = order
          };
          context.Documents.Add(document);
          context.SaveChanges();
          tran.Commit();
      }
      catch (Exception ex)
      {
          tran.Rollback();
      }
  }
  </code></pre>