title: MongoDB serialize Dictionary<MyEnum,object>
body: <p>I have model with some <code>Dictionary&lt;MyEnum, object&gt;</code>.
  When I try to insert in mongoDB with the C# driver, an exception occure with the following message :</p>
  
  <blockquote>
    <p>When using DictionaryRepresentation.Document key values must serialize as strings.</p>
  </blockquote>
  
  <p>Of course, I can add the attribute <code>[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]</code> and it work, but I want to be able to persist the enum as string.</p>
  
  <pre><code>public MyEnum {
    A,
    B
  }
  
  [BsonDictionaryOptions(DictionaryRepresentation.Document)]
  public Dictionary&lt;MyEnum, object&gt; MyData { get; set; }
  </code></pre>
  
  <p>I want to have Something like that in mongo for different reasons.</p>
  
  <pre><code>{
     "MyData": {
        "B": "xxxx",
        "A": "xxxx"
     }
  }
  </code></pre>
  
  <p>for a single enum, I just can use the <code>[BsonRepresentation(BsonType.String)]</code> attribute, but how to tell to the driver for a dictionary to serialize the enum key as string?</p>