Element name
To specify an element name using attributes, write:
C#
public class MyClass {
[BsonElement("sp")]
public string SomeProperty { get; set; }
}The same result can be achieved without using attributes with the following initialization code:
C#
BsonClassMap.RegisterClassMap<MyClass>(cm => {
cm.AutoMap();
cm.GetMemberMap(c => c.SomeProperty).SetElementName("sp");
});Element order
C#
public class MyClass {
[BsonElement("sp", Order = 1)]
public string SomeProperty { get; set; }
}
BsonClassMap.RegisterClassMap<MyClass>(cm => {
cm.AutoMap();
cm.GetMemberMap(c => c.SomeProperty).SetElementName("sp").SetOrder(1);
});Identifying the Id field or property
C#
public class MyClass {
[BsonId]
public string SomeProperty { get; set; }
}
BsonClassMap.RegisterClassMap<MyClass>(cm => {
cm.MapIdProperty(c => c.SomeProperty);
// mappings for other fields and properties
});Ignoring null values
C#
public class MyClass {
[BsonIgnoreIfNull]
public string SomeProperty { get; set; }
}Or using initialization code instead of attributes:
C#
BsonClassMap.RegisterClassMap<MyClass>(cm => {
cm.AutoMap();
cm.GetMemberMap(c => c.SomeProperty).SetIgnoreIfNull(true);
});

Join the conversation! Your thoughts help the community grow.