Tengo un documento que se representa usando esta clase.
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; } }Cuando trato de usar el controlador C# para insertar un documento, aparece el siguiente error:
MongoDB.Bson.BsonSerializationException: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified. Intenté configurar GuidRepresentation haciendo lo siguiente:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard)] public Guid[] Attachments { get; }Después de hacer esto me sale
System.InvalidOperationException: [BsonGuidRepresentationAttribute] can only be used when the serializer is a GuidSerializer.Entonces pensé, configuraré el serializador:
[BsonElement, BsonGuidRepresentation(GuidRepresentation.Standard), BsonSerializer(typeof(GuidSerializer))] public Guid[] Attachments { get; }Lo que luego conduce a:
System.ArgumentException: Value type of serializer is System.Guid and does not match member type System.Guid[]. (Parameter 'serializer')¿Que me estoy perdiendo aqui?
También tenga en cuenta que he establecido las siguientes configuraciones globalmente, al inicio:
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;Una alternativa que funciona, pero no me gusta mucho es esta
[BsonElement, BsonRepresentation(BsonType.String)] public Guid[] Attachments { get; }¿No hay forma de almacenar Guids en una matriz en MongoDB?
Agregar la siguiente línea al código de inicio solucionó el problema.
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));