title: Creating MongoDB Unique Key with C#
body: <p>I am fighting to create a unique field <code>EmailAddress</code>. I've already seen in forums that I have to create an index, but it didn't work out for me so far. Does anyone have a code example? Do I have to create the index on every save/call, or is it enough to create it only once? </p>
  
  <p>I tried this code:</p>
  
  <pre><code>DB.GetCollection&lt;User&gt;(Dbname)
      .EnsureIndex(new IndexKeysBuilder()
          .Ascending("EmailAddress"), IndexOptions.SetUnique(true));
  
  DB.GetCollection&lt;User&gt;(Dbname).Save(user, SafeMode.True);
  </code></pre>
  
  <p>My <code>User</code> model looks like this:</p>
  
  <pre><code>public class User
  {
      [Required(ErrorMessage = "Email Required")]
      public string EmailAddress { get; set; }
  
      public ObjectId Id { get; set; }
  
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  </code></pre>