mongodb c# 驱动程序 - 继承、映射和序列化问题

mongodb c# driver - inheritance, mappings and serialization issue

对于存储在 mongodb 中的对象,我有以下 class 层次结构(我只在他们的图表中存储分支对象和实体)

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; }
}

我在服务器启动时针对此层次结构进行了以下注册 运行:

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

但是当我运行这个查询

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();

我遇到以下异常:

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)

这是什么原因?映射不正确或在错误的时间调用?

根据 mongodb 开发人员的说法,我曾就此问题联系过

  1. 异常不提供信息
  2. 尚不支持第一个语句

请参阅此 ticket 了解更多信息。

将在 2.0.1 及更高版本中进行改进以解决此问题。