まず、現在のバージョンのMongoDB(3.2)とC#-driver(2.2.3)を使用します。 は、私は次のクラスがあります。C#を使用したMongoDBのインタフェースの逆シリアル化 - 不明な識別子値
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<string, object> PayloadData { get; }
}
をそれから私はIBaseDataインタフェースのいくつかの異なる実装を持っている:MongoDBのに項目オブジェクトを格納する
public class EventData : IBaseData
{
public int Version { get; set; }
public string Name { get; set; }
public IDictionary<string, object> PayloadData { get; set; }
public IDictionary<string, object> Properties { get; set; }
}
public class ExceptionData : IBaseData
{
// Implementation of the interface and some additional properties
}
は問題ありません、すべてが正しいようです。 Mongoは_t
を使用してIBaseData
のタイプを判定します(例:EventData
)。私が最初にデータを取得しようとすると、それは完全に動作します。つまり、オブジェクトの完全なツリーがあります。私は、アプリケーションを再起動して、もう一度同じ要求を投稿すると、私は次のエラーを取得する:予想通り
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'.
モンゴ・データベースをドロップし、アプリケーションを起動した後、それが再び動作します。
Googleは私に異なるアプローチを与えたが、何も私を助けていない:
Storing a Dictionary with polymorphic values in mongoDB using C#
Unknown discriminator value 'MyEvent'
Deserialize object as an interface with MongoDB C# Driver
私は[BsonKnownTypes(typeof(Type1), typeof(Type2)]
のようなプロパティの注釈と協力したいと思いますが、これはただのクラスのために働きます。
アイデアはありますか?
ポリの代わりに動的BSonDocumentを使用することを検討しましたか? –