Serializing a covariant override with MongoDB in C#

Viewed 106

I have a base class similar to:

public class Entity
{
    public virtual object Id { get; set; }
}

And concrete classes override the property with more specific types, using C#'s covariant return type feature:

public class User : Entity
{
    public override string Id { get; set; }
}

(This is a simplification of my situation, I'm not asking for a better model but only how to persist it)

Now my problem is that the Mongo BSON serializer thinks these are two separate properties, and it throws an exception because there are two properties that map to the same element.

How can I persist such a model (where properties are overridden with more specific types, assuming the types of the properties are serializable)?

1 Answers

AFAIK, the type of the property should be the same, otherwise it's 2 different properties, you may try to archive this behavior with custom convention, where you will need to ensure that all Members are registered correctly.

Related