[CSHARP-1299] NamedIdMemberConvention should do nothing if base class already has an _id Created: 28/May/15  Updated: 05/Apr/19  Resolved: 21/Mar/17

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.10, 2.0
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related

 Description   

Serializing the following classes fails because NamedIdMemberConvention doesn't realize that C already has an _id, and tries to map the Id in D to "_id" also, resulting in duplicate element names.

public class C
{
    public int _id { get; set; }
}
 
public class D : C
{
    public int Id { get; set; }
}



 Comments   
Comment by Robert Stam [ 21/Mar/17 ]

The Id in the derived D class must have a different name.

You could use the BsonElement attribute to tell the driver what name to use instead:

public class D : C
{
    [BsonElement("Id")]
    public int Id { get; set; }
}

Generated at Wed Feb 07 21:39:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.