Details
-
Bug
-
Resolution: Works as Designed
-
Critical - P2
-
None
-
2.5
-
None
-
netstandard 2.0
Description
Imagine a class to store in a collection :
enum MyEnum |
{
|
Value1
|
}
|
|
|
class MyDoc |
{
|
public string Id { get; set; } |
public MyEnum Prop { get; set; } |
public MyEnum Prop2 |
{
|
get { return MyEnum.Value1; } |
}
|
}
|
Mapping is declared as it to allow serialization of read only property :
BsonClassMap.RegisterClassMap<MyDoc>(cm =>
|
{
|
cm.AutoMap();
|
cm.MapIdProperty(x => x.Id)
|
.SetSerializer(new StringSerializer(BsonType.ObjectId)) |
.SetIdGenerator(StringObjectIdGenerator.Instance);
|
cm.MapMember(x => x.Prop2);
|
});
|
These conventions are added :
var pack= new ConventionPack(); |
pack.Add(new CamelCaseElementNameConvention()); |
pack.Add(new EnumRepresentationConvention(BsonType.String)); |
ConventionRegistry.Register("custom", pack, t => true); |
When this instance is inserted into the collection:
var doc = new MyDoc |
{
|
Prop = MyEnum.Value1
|
}
|
Then the resulting document in MongoDB is :
{
|
"_id":"5a7f07f1ac141637ec1ba0c9",
|
"prop": "Value1",
|
"Prop2": 0
|
}
|
The conventions are not respected for the read only property Prop2 :
- its name is not camel case (first letter is upper case)
- its value is stored with the enum value instead of enum key.