mongodb c#driver - 继承,映射和序列化问题

时间:2015-04-16 09:16:27

标签: c# .net mongodb mongodb-.net-driver nosql

我对mongodb中存储的对象有以下类层次结构(我只在其图形中存储了Branch对象和实体)

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

我遇到以下异常:

  

测试方法ShouldGetBranchToolTipAsync抛出异常:

     

System.ArgumentOutOfRangeException:memberInfo参数必须是   对于类Branch,但是对于类Aggregate。

     

参数名称:memberInfo       在MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo)   在   MongoDB.Bson.Serialization.BsonClassMap.MapMember(的MemberInfo的MemberInfo)   在   MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildProjectedSerializer(ProjectionMapping映射)   在   MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildMemberInit(MemberInitExpression节点)   在   MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(表达式节点)   在   MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(表达式节点,IBsonSerializerRegistry serializerRegistry)   在   MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.TranslateProject(表达式1 projector, IBsonSerializer 1 ParameterSerializer把,IBsonSerializerRegistry serializerRegistry)   在   MongoDB.Driver.IAggregateFluentExtensions.ProjectExpressionProjection 2.Render(IBsonSerializer 1 documentSerializer,IBsonSerializerRegistry serializerRegistry)

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

1 个答案:

答案 0 :(得分:2)

根据mongodb开发者的说法,我已经联系过这个问题

  1. 例外不提供信息
  2. 尚不支持第一条声明
  3. 有关详细信息,请参阅此ticket

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