title: MongoDB C# 2.1 ReplaceOneAsync not working for repository pattern
body: <p>I'm trying to implement async repository pattern for mongodb. This is my code:</p>
  
  <pre><code>public class EntityBase
  {
      [BsonId]
      public ObjectId Id { get; set; }
  }
  
  public class User : EntityBase
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  
  public class MongoDbRepository&lt;T&gt; where T : EntityBase
  {
      private IMongoDatabase _database;
      private IMongoCollection&lt;T&gt; _collection;
  
      public MongoDbRepository()
      {
          GetDatabase();
          GetCollection();
      }
  
      private void GetDatabase()
      {
          var client = new MongoClient("mongodb://localhost/");
          _database = client.GetDatabase("LocalTest");
      }
  
      private void GetCollection()
      {
          _collection = _database.GetCollection&lt;T&gt;(typeof(T).Name);
      }
  
      public async Task&lt;ReplaceOneResult&gt; Save(T doc)
      {
          return await _collection.ReplaceOneAsync(w =&gt; w.Id == doc.Id, doc, new UpdateOptions { IsUpsert = true });
      }
  }
  </code></pre>
  
  <p>And console application which just calling the logic:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var userRepo = new MongoDbRepository&lt;User&gt;();
  
          userRepo.Save(new User
          {
              FirstName = "fn",
              LastName = "ln"
          }).Wait();
  
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>In this line of code:</p>
  
  <pre><code>return await _collection.ReplaceOneAsync(w =&gt; w.Id == doc.Id, doc, new UpdateOptions { IsUpsert = true });
  </code></pre>
  
  <p>I'm getting an error:</p>
  
  <blockquote>
    <p>Convert([document]).Id is not supported.</p>
  </blockquote>
  
  <p>If I'm changing the code like this, it's working:</p>
  
  <pre><code>private IMongoCollection&lt;EntityBase&gt; _collection;
      private void GetCollection()
      {
          _collection = _database.GetCollection&lt;EntityBase&gt;(typeof(EntityBase).Name);
      }
  </code></pre>
  
  <p>But I really need to use T instead of BaseMongoClass...
  Any ideas why this error occurs and how can I fix it?
  Thanks a lot!</p>