简体   繁体   English

MongoDB C# 2.0 Driver SetSerializationOptions

[英]MongoDB C# 2.0 Driver SetSerializationOptions

I have updated C# MongoDB Driver to 2.0我已经将 C# MongoDB 驱动程序更新到 2.0

Now SetSerializationOptions is not working.现在SetSerializationOptions不起作用。 Following is the old code.以下是旧代码。

cm.GetMemberMap(m => m.ArrayField)
      .SetSerializationOptions(
          new ArraySerializationOptions(
                new RepresentationSerializationOptions(BsonType.ObjectId)));

Please help me with the replacement for above code in 2.0 Driver?请帮我替换 2.0 驱动程序中的上述代码?

The new documentation covers this here: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#serialization-options . 新文档在此处进行了介绍: http : //mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#serialization-options

You need to set a new serializer that is configured appropriately for your scenario. 您需要设置一个新的序列化程序,该序列化程序已针对您的方案进行了适当配置。

Craig 克雷格

Working code for MongoDB.Driver 2.14.1, might help anyone MongoDB.Driver 2.14.1 的工作代码,可能对任何人都有帮助

public class Document 
{
    public string[] ArrayField {get; set;}
}

BsonClassMap.RegisterClassMap<Document>(cm =>
{
    cm.MapMember(m => m.ArrayField)
        .SetSerializer(
            new ArraySerializer<string>(new StringSerializer(BsonType.ObjectId))
        );
});

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

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