Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2550

MongoDb c# driver mapping on derived class for base class members

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Hi! 

      I have a such class structure:

      public abstract class Entity : IEntity
      {
          [BsonId]
          [JsonIgnore]
          private ObjectId _id;
          
          protected Entity()
          {
              Id = Guid.NewGuid().ToString();
          }
      
          public string Id { get; private set; }
      }
      
      public class Device : Entity
      {
       //Some fields
      }
      
      public class User : Entity
      {
       //Some fields
      }

      Basically what i would like to do, is class mapping (via convention), which map Id field on base type Entity for each derived type differently in that convention:

      Id -> entityId

      Examples:

       

      Id -> deviceId for Device Document
      Id -> userId for User Document
      

      I tried using IPostProcessingConvention, but when i request Mongo for Device object, it first doing map for Device class, and then for base type. AllMapMembers collection is empty. BaseClassMap is null.

      I thought that this is due to incorrect ordering which auto-resolver map classes (first Derived Type, then base type), but when i forced this manually (by requesting first Entity, and then Device, mapping launch in correct oder that way),  AllMapMembers still are empty, and BaseClassMap is null. 

      It seems that something is broken, or maybe i misunderstood those properties.

      Can you explain for what reason are there, and if my mapping rules are achievable in current version of MongoDB C# driver? 

            Assignee:
            wan.bachtiar@mongodb.com Wan Bachtiar
            Reporter:
            kurakmen@gmail.com Jakub Dropia
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: