MongoDB的C#的驱动程序 - 继承,映射和序列化问题 [英] mongodb c# driver - inheritance, mappings and serialization issue
问题描述
我对存储在MongoDB的对象类层次结构(我只存储分行对象和实体在图)
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:
测试方法ShouldGetBranchToolTipAsync抛出异常:
Test method ShouldGetBranchToolTipAsync threw exception:
System.ArgumentOutOfRangeException:在的MemberInfo参数必须是 类科,但对类集合。
System.ArgumentOutOfRangeException: The memberInfo argument must be for class Branch, but was for class 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(MemberInitEx$p$pssion 节点)
在
MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(前pression节点)
在
MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(前pression节点,IBsonSerializerRegistry serializerRegistry)
在
MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.TranslateProject(前pression 1台投影仪,IBsonSerializer
1 ParameterSerializer把,IBsonSerializerRegistry serializerRegistry)
在
MongoDB.Driver.IAggregateFluentExtensions.ProjectEx$p$pssionProjection<$c$c>2.Render(IBsonSerializer$c$c>1 documentSerializer,IBsonSerializerRegistry serializerRegistry)
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, IBsonSerializer
1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
at
MongoDB.Driver.IAggregateFluentExtensions.ProjectExpressionProjection2.Render(IBsonSerializer
1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
什么是这个原因?正在映射不正确或称为在错误的时间?
What is the cause of this? Are mapping incorrect or called at a wrong time?
推荐答案
据MongoDB的开发者,我接触这个问题。
According to mongodb developers I've contacted with this issue
- 在异常不翔实
- 在不支持先声明尚未
请参阅此票获取更多的信息。
Please see this ticket for more info.
改进将作出解决这一问题在2.0.1及以后。
Improvements will be made to fix this in 2.0.1 and beyond.
这篇关于MongoDB的C#的驱动程序 - 继承,映射和序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!