MongoDb class map registration

I have the following code. I want MiscellaneousData to override the abstract MiscellaneousDataBase. However, IdMemberMap is always null.

The autonomous "normal" class works.

if (!BsonClassMap.IsClassMapRegistered(typeof(MiscellaneousData))) { BsonClassMap.RegisterClassMap<MiscellaneousData>(cm => { cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(c => c.Key)); cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance); }); } 

Here are the different data and its base - reduced for clarity:

 public abstract class MiscellaneousDataBase { [XmlIgnore] public abstract string Key { get; set; } } public class MiscellaneousData : MiscellaneousDataBase { public override string Key { get; set; } } 
+2
source share
1 answer

When creating class maps, you map each member at the level at which it is declared, so you need to map MiscellaneousDataBase and MiscellaneousData separately.

Below is a code example showing how to map each class separately (I added the X property to the subclass so that there is something to display at this level).

Using these classes:

 public abstract class MiscellaneousDataBase { public abstract string Key { get; set; } } public class MiscellaneousData : MiscellaneousDataBase { public override string Key { get; set; } public int X { get; set; } } 

try matching them like this:

 BsonClassMap.RegisterClassMap<MiscellaneousDataBase>(cm => { cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(c => c.Key)); cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance); }); BsonClassMap.RegisterClassMap<MiscellaneousData>(cm => { cm.AutoMap(); cm.GetMemberMap(c => cX).SetElementName("x"); }); 
+3
source

Source: https://habr.com/ru/post/1382098/


All Articles