2012-02-28 65 views
0

“反序列化类Class1的Property1属性时发生错误:ReadString只能在CurrentBsonType为String时调用,而不能在CurrentBsonType为ObjectId时调用。CurrentBsonType是字符串,而不是当CurrentBsonType是ObjectId时

我把bsonid放在类的字符串guid上,映射了类并将其序列化,但是当我试图从bsondocument将对象反序列化到类时,它给了我这个错误。

我该如何解决这个问题?

public class Class1 
{ 
     [BsonId] 
     public string ClassGuid { get; private set; } 
} 

然后映射类:

if (!BsonClassMap.IsClassMapRegistered(typeof(WordOntology))) 
{ 
     BsonClassMap.RegisterClassMap<WordOntology>(); 
} 

然后反序列化

MongoCursor<Class1> _returnResults = _collection.FindAs<Class1>(_query); 

然后循环:

foreach(BsonDocument _document in _returnResults) 
{ 
    //exception here 
} 

回答

0

你可能有您的收藏现有文件不匹配你的班级定义。你可以使用mongo shell查找它们(查找有字符串的ObjectId的文档)。

如果这样不能解释它,你能提供你的类声明和一个不会正常反序列化的示例文件(使用mongo shell显示)吗?

+0

我添加了更多的课程信息。 – iefpw 2012-02-28 20:34:54

+0

我试图反序列化到错误的类。 – iefpw 2012-02-29 00:27:14

+0

现在它适合你吗?如果您有任何后续问题,请告知我。 – 2012-02-29 18:11:20

相关问题