Deserializing interface in MongoDB using C# - Unknown discriminator value
<p>First of all, I use the current version of MongoDB (3.2) and its C#-driver (2.2.3).
I have the following classes:</p>

<pre><code>public class Item
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    // ... some other properties

    public Data Data { get; set; }
}

public class Data
{
    public string BaseType { get; set; }
    public IBaseData BaseData { get; set; }
}

public interface IBaseData 
{
    string Name { get; set; }
    int Version { get; set; }
    IDictionary&lt;string, object&gt; PayloadData { get; }
}
</code></pre>

<p>Then I have some different implementations of the IBaseData interface:</p>

<pre><code>public class EventData : IBaseData
{
    public int Version { get; set; }
    public string Name { get; set; }
    public IDictionary&lt;string, object&gt; PayloadData { get; set; }
    public IDictionary&lt;string, object&gt; Properties { get; set; }
}

public class ExceptionData : IBaseData
{
    // Implementation of the interface and some additional properties
}
</code></pre>

<p>Storing an Item-object to MongoDB is no problem and it seems that everything is correct. Mongo uses the <code>_t</code> to determine the type of <code>IBaseData</code> (e.g. <code>EventData</code>). When I try to retrieve the data for the first time, it works perfectly which means I have the complete tree of objects. <strong>When I restart the application</strong> and post the same request again, I get the following error:</p>

<blockquote>
  <p>An error occurred while deserializing the Data property of class Domain.Objects.Item: An error occurred while deserializing the BaseData property of class Domain.Objects.Data: Unknown discriminator value 'EventData'.</p>
</blockquote>

<p>After dropping the Mongo-database and starting the application, it works again as expected.</p>

<p>Google gave me different approaches but nothing helped me:</p>

<p><a href="https://stackoverflow.com/questions/8138330/storing-a-dictionary-with-polymorphic-values-in-mongodb-using-c-sharp">Storing a Dictionary with polymorphic values in mongoDB using C#</a></p>

<p><a href="https://stackoverflow.com/questions/7451422/unknown-discriminator-value-myevent">Unknown discriminator value &#39;MyEvent&#39;</a></p>

<p><a href="https://stackoverflow.com/questions/14561809/deserialize-object-as-an-interface-with-mongodb-c-sharp-driver?rq=1">Deserialize object as an interface with MongoDB C# Driver</a></p>

<p>I would like to work with property annotations like <code>[BsonKnownTypes(typeof(Type1), typeof(Type2)]</code>, but this just works for classes.</p>

<p>Do you have an idea?</p>