1

考虑以下 C# 类:

public class Role
{
    public string Id { get; set; }
    public IEnumerable<string> Users { get; set; }
}

如何使用配置将用户映射到 MongoDB 中的 ObjectIds 数组BsonClassMap.RegisterClassMap<Role>?我能够配置标量属性 Id 映射到 ObjectId,但无法弄清楚如何对序列(数组)执行相同操作:

BsonClassMap.RegisterClassMap<Role>(m =>
    {
        m.MapIdProperty(r => r.Id).SetRepresentation(BsonType.ObjectId);
        // How do I map r.Users to an array of ObjectId??
        m.MapProperty(r => r.Users);
     });
4

1 回答 1

2

答案是SetSerializationOptions使用正确初始化的ArraySerializationOptions对象进行调用:

BsonClassMap.RegisterClassMap<Role>(m =>
    {
        m.MapIdProperty(r => r.Id).SetRepresentation(BsonType.ObjectId);
        m.MapProperty(r => r.Users).SetSerializationOptions(
                new ArraySerializationOptions(new
                        RepresentationSerializationOptions(BsonType.ObjectId)));
     });
于 2013-01-24T16:25:12.643 回答