title: MongoDB C#: ID Serialization best pattern
body: <p>I have a class <code>User</code> and I need to work with them in web services.</p>
  
  <p>Then problem is that if I try to serialize <code>Id</code> that is type of <code>BsonObjectId</code>, I see
  that have an empty property, that have an empty property, and so on ...</p>
  
  <p>I have write this workaround in order, it's is a good solution?</p>
  
  <pre><code>public partial class i_User 
  {
      [BsonId(IdGenerator = typeof(BsonObjectIdGenerator))]
      [NonSerialized]
      public BsonObjectId _id;
  
      public String Id
      {
          get
          {
              return this._id.ToString();
          }
      }   
  }   
  </code></pre>
  
  <p>In this way, I can keep <code>_Id</code> as <code>BsonObjectId</code> but I send an string representation over the web in the property <code>Id</code>.</p>
  
  <p>Another solution is to work with <code>StringObjectIdGenerator</code></p>
  
  <pre><code>public partial class i_User 
  {
      [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
      public String id;
  }
  </code></pre>
  
  <p>But is see that MongoDB will store a <code>string</code> into database instead of <code>ObjectId</code>.</p>
  
  <p>What is the best approach in order to work in a serialization environmental like web services and/or an client-server (Flash+C#)?</p>