title: MongoDB C# driver - Change Id serialization for inherited class
body: <p>I have implemented Repository pattern with a base entity class for my collections. Till now all collections had <code>_id</code> of <code>ObjectId</code> type. In the code, I needed to represent the <code>Id</code> as a string.</p>
  
  <p>Here is how the <code>EntityBase</code> class look like</p>
  
  <pre><code>public abstract class EntityBase
  {
      [BsonRepresentation(BsonType.ObjectId)]
      public virtual string Id { get; set; }
  }
  </code></pre>
  
  <p>Here is the mapping:</p>
  
  <pre><code>BsonClassMap.RegisterClassMap&lt;EntityBase&gt;(cm =&gt;
  {
      cm.AutoMap();
      cm.MapIdProperty(x =&gt; x.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
      cm.IdMemberMap.SetSerializer(new StringSerializer().WithRepresentation(BsonType.ObjectId));
  });
  </code></pre>
  
  <p>Now I have a Language collection which Id will be plain string something like <code>en-GB</code>.</p>
  
  <pre><code>{
     "_id" : "en-GB",
     "Term1" : "Translation 1",
     "Term2" : "Translation 2"
  }
  </code></pre>
  
  <p><code>Language</code> class is inheriting the <code>EntityBase</code> class</p>
  
  <pre><code>public class Language : EntityBase
  {
      [BsonExtraElements]
      public IDictionary&lt;string, object&gt; Terms { get; set; }
      public override string Id { get; set; }
  }
  </code></pre>
  
  <p>The question is can I somehow change how the <code>Id</code> is serialized only for the <code>Language</code> class? </p>
  
  <p>I don't want to change the behaviour of <code>EntityBase</code> class since I have a lot of other collections inheriting the <code>EntityBase</code>.</p>
  
  <h2>Update</h2>
  
  <p>Here is what I tried and got exception. Not sure if what I have tried is possible.</p>
  
  <pre><code>BsonClassMap.RegisterClassMap&lt;Language&gt;(cm =&gt;
  {
      cm.AutoMap();
      cm.MapExtraElementsMember(c =&gt; c.Terms);
      cm.MapIdProperty(x =&gt; x.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
      cm.IdMemberMap.SetSerializer(new StringSerializer().WithRepresentation(BsonType.String));
  });
  </code></pre>
  
  <p>Here is the exception that I was getting:</p>
  
  <pre><code>An exception of type 'System.ArgumentOutOfRangeException' occurred in MongoDB.Bson.dll but was not handled in user code
  
  Additional information: The memberInfo argument must be for class Language, but was for class EntityBase.
  
  at MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo)
  at MongoDB.Bson.Serialization.BsonClassMap.MapMember(MemberInfo memberInfo)
  at MongoDB.Bson.Serialization.BsonClassMap`1.MapMember[TMember](Expression`1 memberLambda)
  at MongoDB.Bson.Serialization.BsonClassMap`1.MapProperty[TMember](Expression`1 propertyLambda)
  at MongoDB.Bson.Serialization.BsonClassMap`1.MapIdProperty[TMember](Expression`1 propertyLambda)
  at Test.Utilities.MongoDbClassConfig.&lt;&gt;c.&lt;Configure&gt;b__0_1(BsonClassMap`1 cm) in F:\Development\Test\Utilities\MongoDbClassConfig.cs:line 23
  at MongoDB.Bson.Serialization.BsonClassMap`1..ctor(Action`1 classMapInitializer)
  at MongoDB.Bson.Serialization.BsonClassMap.RegisterClassMap[TClass](Action`1 classMapInitializer)
  at Test.Utilities.MongoDbClassConfig.Configure() in F:\Development\Test\Utilities\MongoDbClassConfig.cs:line 20
  at Test.Portal.BackEnd.Startup..ctor(IHostingEnvironment env) in F:\Development\Test\Startup.cs:line 43
  </code></pre>