简体   繁体   English

使用2.0 C#驱动程序将DateTime保存到MongoDb时如何防止丢失精度?

[英]How do I prevent losing precision when saving a DateTime to MongoDb using the 2.0 C# Driver?

Currently when we are serializing and saving an object that contains a DateTime to MongoDb using the 2.0 C# Driver we are losing precision so when it is compared as part of a concurrency check it is failing as the ticks are different. 当前,当我们使用2.0 C#驱动程序将包含DateTime的对象序列化并保存到MongoDb时,我们将失去精度,因此,当将其作为并发检查的一部分进行比较时,由于滴答声不同,因此失败。

I found a blog post that describes how to maintain the precision using the original C# driver by using DateTimeSerializationOptions and specifying a BsonType of Document but I cannot find an example online of how to do this in the new version. 我找到了一篇博客文章 ,其中描述了如何通过使用DateTimeSerializationOptions并指定Document的BsonType来使用原始C#驱动程序保持精度,但是我无法在新版本中在线找到有关如何执行此操作的示例。

What is the equivalent for the code below in the new driver? 以下代码在新驱动程序中的等价物是什么?

DateTimeSerializationOptions.Defaults = new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document);

在GitHub上挖掘了序列化程序的源代码后,我提出了以下工作原理。

BsonSerializer.RegisterSerializer(typeof(DateTime), new DateTimeSerializer(DateTimeKind.Utc, BsonType.Document))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM