Search code examples
c#.netmongodbmongodb-.net-drivernosql

mongodb c# driver - inheritance, mappings and serialization issue


I have following class hierarchy for object stored in mongodb (I store only Branch objects and Entities in their graph)

public class Branch : Aggregate
{
    public IEnumerable<LocalizableText> Description { get; set; }
    public ObjectId PartnerId { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public string Timetable { get; set; }
    public IEnumerable<Discount> Discounts { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Phone> Phones { get; set; }
    public byte[] Icon { get; set; }
    public byte[] Image { get; set; }
}

public abstract class Aggregate : Entity
{
    public ObjectId Id { get; set; }
}

public abstract class Entity
{
    public IEnumerable<LocalizableText> Name { get; set; }
}

I have the following registrations running at server start for this hierarchy:

        BsonClassMap.RegisterClassMap<Entity>();
        BsonClassMap.RegisterClassMap<Aggregate>(cm =>
        {
            cm.AutoMap();
            cm.SetIdMember(cm.GetMemberMap(a => a.Id));
        });
        BsonClassMap.RegisterClassMap<Branch>();

But when I'm running this query

return await Collection.Aggregate().Match(x => x.PartnerId == partnerId)
                                            .Group(x => x.PartnerId, g => new
                                                                            {
                                                                                PartnerId = g.Key,
                                                                                g.First(x => x.Name != null).Name,
                                                                                Description = g.First(x => x.Id == branchId).Name,
                                                                                g.First(x => x.Id == branchId).Discounts,
                                                                                Id = branchId
                                                                            })
                                            .Project(g => new Branch()
                                                            {
                                                                Id = g.Id,
                                                                Name = g.Name,
                                                                Description =  g.Description,
                                                                Discounts = g.Discounts,
                                                                PartnerId = g.PartnerId
                                                            }).FirstOrDefaultAsync();

I'm getting the following exception:

Test method ShouldGetBranchToolTipAsync threw exception:

System.ArgumentOutOfRangeException: The memberInfo argument must be for class Branch, but was for class Aggregate.

Parameter name: memberInfo at MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo) at MongoDB.Bson.Serialization.BsonClassMap.MapMember(MemberInfo memberInfo) at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildProjectedSerializer(ProjectionMapping mapping) at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildMemberInit(MemberInitExpression node) at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(Expression node) at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.TranslateProject(Expression1 projector, IBsonSerializer1 parameterSerializer, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.IAggregateFluentExtensions.ProjectExpressionProjection2.Render(IBsonSerializer1 documentSerializer, IBsonSerializerRegistry serializerRegistry)

What is the cause of this? Are mapping incorrect or called at a wrong time?


Solution

  • According to mongodb developers I've contacted with this issue

    1. Exception is not informative
    2. First statement is not supported yet

    Please see this ticket for more info.

    Improvements will be made to fix this in 2.0.1 and beyond.