MongoDB未知鉴别符值=>反序列化为JSonDocument [英] MongoDB Unknown discriminator value => deserialize to JSonDocument
问题描述
在MongoDB中,我有一个类,其属性为MyProperty,类型为object
.
In MongoDB I've a class with a property MyProperty of type object
.
public MyClass
{
public object MyProperty;
public string Prop1;
public DateTime Prop2;
public int Prop3;
}
对MongoDB的序列化可以毫无问题地创建这种类型的JSon:
Serialization to MongoDB works without any problem creating a JSon of this type:
MyProperty" : {
"_t" : "ExampleClass",
[...]
}
但是当我尝试对其进行序列化时,会出现以下错误:
But when I try do seserialize it I get the following error:
反序列化类的MyProperty属性时发生错误 我的课: 未知的鉴别符值'ExampleClass'.
An error occurred while deserializing the MyProperty property of class MyClass: Unknown discriminator value 'ExampleClass'.
我想将MyProperty反序列化为简单的通用BsonDocument或字符串.
I'd like to deserialize MyProperty to a simple generic BsonDocument or to a string.
推荐答案
_t存储您的自定义类名称.您将需要注册此自定义类映射,以使mongo知道在反序列化时要使用哪个对象.这是一个代码示例(只需要在应用程序的开头调用一次):
The _t stores your custom class name. You will need to register this custom class mapping in order for mongo to know which object to use when deserializing. Here is a code example (should only need to call this once at the beginning of your application):
if (!BsonClassMap.IsClassMapRegistered(typeof(ExampleClass)))
{
BsonClassMap.RegisterClassMap<ExampleClass>();
}
这篇关于MongoDB未知鉴别符值=>反序列化为JSonDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!