C# MongoDB Guid 数组
C# MongoDB Guid Array
我有一个使用此 class
表示的文档
public class UnitDocument
{
[BsonConstructor]
public UnitDocument(Guid id, string allocId, Guid[] attachments)
{
Id = id;
AllocId = allocId;
Attachments = attachments;
}
[BsonId, BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; }
[BsonElement]
public string AllocId { get; }
[BsonElement]
public Guid[] Attachments { get; }
}
当我尝试使用 C# 驱动程序插入文档时出现以下错误:
MongoDB.Bson.BsonSerializationException: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.
我尝试设置 GuidRepresentation
执行以下操作:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid[] Attachments { get; }
完成后我得到
System.InvalidOperationException: [BsonGuidRepresentationAttribute] can only be used when the serializer is a GuidSerializer.
所以我想,我将设置序列化程序:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard), BsonSerializer(typeof(GuidSerializer))]
public Guid[] Attachments { get; }
然后导致:
System.ArgumentException: Value type of serializer is System.Guid and does not match member type System.Guid[]. (Parameter 'serializer')
我在这里错过了什么?
另请注意,我在启动时全局设置了以下设置:
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
一个可行的替代方案,但我不是很喜欢这个
[BsonElement, BsonRepresentation(BsonType.String)]
public Guid[] Attachments { get; }
在MongoDB中有没有办法将Guid存储在数组中?
将以下行添加到 start-up 代码中解决了这个问题。
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
我有一个使用此 class
表示的文档public class UnitDocument
{
[BsonConstructor]
public UnitDocument(Guid id, string allocId, Guid[] attachments)
{
Id = id;
AllocId = allocId;
Attachments = attachments;
}
[BsonId, BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; }
[BsonElement]
public string AllocId { get; }
[BsonElement]
public Guid[] Attachments { get; }
}
当我尝试使用 C# 驱动程序插入文档时出现以下错误:
MongoDB.Bson.BsonSerializationException: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.
我尝试设置 GuidRepresentation
执行以下操作:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid[] Attachments { get; }
完成后我得到
System.InvalidOperationException: [BsonGuidRepresentationAttribute] can only be used when the serializer is a GuidSerializer.
所以我想,我将设置序列化程序:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard), BsonSerializer(typeof(GuidSerializer))]
public Guid[] Attachments { get; }
然后导致:
System.ArgumentException: Value type of serializer is System.Guid and does not match member type System.Guid[]. (Parameter 'serializer')
我在这里错过了什么?
另请注意,我在启动时全局设置了以下设置:
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
一个可行的替代方案,但我不是很喜欢这个
[BsonElement, BsonRepresentation(BsonType.String)]
public Guid[] Attachments { get; }
在MongoDB中有没有办法将Guid存储在数组中?
将以下行添加到 start-up 代码中解决了这个问题。
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));