Nullable<Guid> doesn't work with [BsonGuidRepresentation(GuidRepresentation.Standard)]

Hello,

I have an issue with Guid? (Nullable) serialization.

I have migrated to Mongo C# Driver v3.1 and since it doesn’t have a way to specify Guid serialization globally, I have specified it for each field with attribute.

I have migrated my LegacyCSharpUids to Standard UUID.

I have an issue with Nullable fields.

I have an error:

System.InvalidOperationException: [BsonGuidRepresentationAttribute] can only be used when the serializer is a GuidSerializer.

Everything works fine for not nullable Guids

Can please anyone help me?

Hi @Illia_Hryshkevych, thank a lot for your post. I can confirm that this is indeed a bug, and I’ve created a ticket for it: https://jira.mongodb.org/browse/CSHARP-5456

Regarding your use case, if you’re using the same representation on all the fields you can also register a static serializer:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

This will work also on nullable Guid fields and you won’t need to use the add the attribute on all fields.

1 Like

Thanks! It worked!

I first made a mistake and registered a serializer after first access to the collection and it was failing, but then moved it to be first

1 Like

Glad to hear it worked! We’re also working on fixing the issue you’ve seen in the next version.