mongodb c# driver - inheritance, mappings and serialization issue

杀马特。学长 韩版系。学妹 提交于 2019-12-10 21:37:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29670319/mongodb-c-sharp-driver-inheritance-mappings-and-serialization-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!