C# 如何使用BsonClassMap配置ObjectId数组的表示?
考虑以下C类:C# 如何使用BsonClassMap配置ObjectId数组的表示?,c#,.net,mongodb,serialization,bson,C#,.net,Mongodb,Serialization,Bson,考虑以下C类: 公共类角色 { 公共字符串Id{get;set;} 公共IEnumerable用户{get;set;} } 如何配置用户使用BsonClassMap.RegisterClassMap映射到MongoDB中的ObjectID数组?我能够配置标量属性Id映射到ObjectId,但不知道如何对序列(数组)执行相同的操作: BsonClassMap.RegisterClassMap(m=> { m、 MapIdProperty(r=>r.Id).SetRepresentation(Bs
公共类角色
{
公共字符串Id{get;set;}
公共IEnumerable用户{get;set;}
}
如何配置用户使用BsonClassMap.RegisterClassMap
映射到MongoDB中的ObjectID数组?我能够配置标量属性Id映射到ObjectId,但不知道如何对序列(数组)执行相同的操作:
BsonClassMap.RegisterClassMap(m=>
{
m、 MapIdProperty(r=>r.Id).SetRepresentation(BsonType.ObjectId);
//如何将r.用户映射到ObjectId数组??
m、 MapProperty(r=>r.Users);
});
答案是使用正确初始化的数组序列化选项
对象调用设置序列化选项
:
BsonClassMap.RegisterClassMap<Role>(m =>
{
m.MapIdProperty(r => r.Id).SetRepresentation(BsonType.ObjectId);
m.MapProperty(r => r.Users).SetSerializationOptions(
new ArraySerializationOptions(new
RepresentationSerializationOptions(BsonType.ObjectId)));
});
BsonClassMap.RegisterClassMap(m=>
{
m、 MapIdProperty(r=>r.Id).SetRepresentation(BsonType.ObjectId);
m、 MapProperty(r=>r.Users).SetSerializationOptions(
新阵列序列化选项(新
RepresentationSerializationOptions(BsonType.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);
});
BsonClassMap.RegisterClassMap<Role>(m =>
{
m.MapIdProperty(r => r.Id).SetRepresentation(BsonType.ObjectId);
m.MapProperty(r => r.Users).SetSerializationOptions(
new ArraySerializationOptions(new
RepresentationSerializationOptions(BsonType.ObjectId)));
});