Skip to content

Commit 33b3d58

Browse files
committed
correct documentation entries about representation.
1 parent 0cae65f commit 33b3d58

File tree

1 file changed

+31
-2
lines changed
  • Docs/reference/content/reference/bson/mapping

1 file changed

+31
-2
lines changed

Docs/reference/content/reference/bson/mapping/index.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,13 @@ Or via code:
591591
BsonClassMap.RegisterClassMap<MyClass>(cm =>
592592
{
593593
cm.AutoMap();
594-
cm.GetMemberMap(c => c.RepresentAsInt32).SetRepresentation(BsonType.Int32);
595-
cm.GetMemberMap(c => c.RepresentAsString).SetRepresentation(BsonType.String);
594+
cm.MapMember(c => c.RepresentAsInt32).SetSerializer(new CharSerializer(BsonType.Int32));
595+
cm.MapMember(c => c.RepresentAsString).SetSerializer(new CharSerializer(BsonType.String));
596596
});
597597
```
598598

599+
#### ObjectIds
600+
599601
One case that deserves special mention is representing a string externally as an ObjectId. For example:
600602

601603
```csharp
@@ -616,6 +618,33 @@ BsonClassMap.RegisterClassMap<Employee>(cm =>
616618
});
617619
```
618620

621+
#### Enums
622+
623+
Another case that deserves mention is enumerations. Enumerations are, by default, represented as their base value. In other words, a plain enum will be represented as an integer value. However, it is possible to instruct the driver to represent an enum as a string.
624+
625+
```csharp
626+
public enum Color
627+
{
628+
Blue,
629+
Other
630+
}
631+
632+
public class Person
633+
{
634+
[BsonRepresentation(BsonType.String)]
635+
public Color FavoriteColor { get; set; }
636+
}
637+
```
638+
639+
Or via code:
640+
641+
```csharp
642+
BsonClassMap.RegisterClassMap<Person>(cm =>
643+
{
644+
cm.AutoMap();
645+
cm.MapMember(c => c.FavoriteColor).SetSerializer(new EnumSerializer<Color>(BsonType.String));
646+
});
647+
```
619648

620649
## Custom Attributes
621650

0 commit comments

Comments
 (0)