[CSHARP-2550] MongoDb c# driver mapping on derived class for base class members Created: 18/Mar/19 Updated: 02/Apr/20 Resolved: 02/Apr/20 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Jakub Dropia | Assignee: | Wan Bachtiar |
| Resolution: | Done | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Hi! I have a such class structure:
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:
Examples:
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? |
| Comments |
| Comment by Wan Bachtiar [ 24/May/19 ] | |||||||||||||||||||
Hi Jakub, Could you clarify what you would like to achieve by providing a snippet code on how you would use the class mappings ? Are you wanting a way to dynamically change Id attribute for each of the derived class ? For example, the Id attribute is DeviceId instead of Id from the parent ? If so, perhaps the following is what you're looking for:
Then you can use:
Note that in order for auto-mapping to map a field it has to be read-write. The private set in your example may be preventing to do what you're trying to achieve. Regards, |