How can I tell the MongoDB C# driver to store all Guids in string format?

前端 未结 4 622
逝去的感伤
逝去的感伤 2021-01-13 08:47

I\'m currently applying the [BsonRepresentation(BsonType.String)] attribute to all Guid properties in my domain models to have those properties ser

4条回答
  •  一生所求
    2021-01-13 09:17

    This can be achieved using Conventions

    Something along the lines of:

    var myConventions = new ConventionProfile();
    myConventions.SetSerializationOptionsConvention(
        new TypeRepresentationSerializationOptionsConvention(typeof (Guid), BsonType.String));
    
    BsonClassMap.RegisterConventions(myConventions, t => t == typeof (MyClass));
    

    This should go somewhere in your app startup.

    You can read more about conventions here: http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Conventions

提交回复
热议问题