简体   繁体   English

如何使用Mongo C#驱动程序的BSONClassMap将所有属性序列化和反序列化为String

[英]How do I Serialize and DeSerialize all properties to String using Mongo C# Driver's BSONClassMap

BsonClassMap.RegisterClassMap<X>(cm => { cm.AutoMap(); cm.GetMemberMap(x => x.date).SetSerializer(new dateSerializer()); BsonClassMap.RegisterClassMap <X>(cm => {cm.AutoMap(); cm.GetMemberMap(x => x.date).SetSerializer(new dateSerializer());

  }); 

I am getting an error that item already contains a key and if I remove the serializer , I get the error ReadBsonType can only be called when state is Type and not when state is Value" 我收到一条错误消息,即该项目已经包含一个密钥,并且如果我删除了序列化程序,我将收到错误ReadBsonType,该错误只能在state为Type时调用,而在state为Value时不能调用。

How do I debug this? 我该如何调试?

You likely have two problems. 您可能有两个问题。

1) Item already contains a key. 1)项目已经包含一个密钥。 You need to make sure that class map registration occurs before any communication with the server occurs. 您需要确保在与服务器进行任何通信之前进行类映射注册。 This error is likely caused by serialization taking place for X before this RegisterClassMap is called. 此错误很可能是由在调用此RegisterClassMap之前X发生的序列化引起的。 If you are sure this isn't true, a stack trace would be helpful. 如果您确定这不是真的,那么堆栈跟踪会有所帮助。

2) Your implementation of dateSerializer is likely incorrect. 2)您实现的dateSerializer可能不正确。 If you could post your implementation, we'd be able to help debug it. 如果您可以发布实施,我们将能够帮助对其进行调试。 Probably warrants a separate question. 可能需要一个单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用BsonClassMap更改MongoDB C#Driver类反序列化的方式? - How can I use BsonClassMap to change the way a MongoDB C# Driver class is deserialized? 使用官方C#驱动程序序列化/反序列化MongoDB Bson文档 - Serialize/Deserialize MongoDB Bson document using Official C# driver 如何使用 Mongo.Driver.Linq 和 Mongo C# 驱动程序 2.3 返回带有过滤子文档的文档? - How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3? 如何使用c#驱动程序复制mongo数据库及其所有集合? - How to copy mongo database with all it's collection using c# driver? 如何通过在 mongo C# 驱动程序中仅使用连接字符串来`ListCollections`? - How to `ListCollections` by using only connection string in mongo C# driver? 如何使用mongo db c#驱动程序执行“和”查询? - How to do “And” queries using mongo db c# driver? Mongo C# 驱动程序:反序列化 BsonValue - Mongo C# Driver: Deserialize BsonValue 使用mongo C#驱动程序,如何序列化自定义对象数组以便存储它? - Using the mongo C# driver, how to serialize an array of custom object in order to store it? 如何让 C# Mongo 驱动程序使用 Linq 表达式比较 DateTime 字段? - How do I get C# Mongo driver to compare a DateTime field using Linq expressions? 如何在C#中反序列化/序列化Microlife的xml - How to deserialize/serialize Microlife's xml in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM