Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
Matriz Guid de C# MongoDB

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Agregar la siguiente línea al código de inicio solucionó el problema.

 BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!