3

I need to define a composite key (_id) on an existing class (MyClass), which is composed of 2 class properties: Id & Version. Also, it has to be done via the BsonClassMap API (and not property attributes), since I cannot change the class code.

I tried something like this, but the second call to SetIdMember() just overrides the first call and doesn't add it to the key:

        BsonClassMap.RegisterClassMap<MyClass>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);
            cm.SetIdMember(cm.GetMemberMap(t => t.Id));
            cm.SetIdMember(cm.GetMemberMap(t => t.Version)); // after this line, the ID seems to be just Version and not Id+Version
        });
Metheny
  • 1,112
  • 1
  • 11
  • 23
  • did you extend your class? so you could use other class' properties. – sashiksu May 05 '19 at 11:11
  • @SashIKa: not sure I understood your comment. The class hasn't changed, I just want to map it correctly to its document representation. – Metheny May 05 '19 at 11:20

0 Answers0