[英]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) 对于存储在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; }
}
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:
测试方法ShouldGetBranchToolTipAsync抛出异常:
System.ArgumentOutOfRangeException: The memberInfo argument must be for class Branch, but was for class Aggregate.
System.ArgumentOutOfRangeException:memberInfo参数必须是类Branch,但是对于类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(Expression
1 projector, IBsonSerializer
1 parameterSerializer, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.IAggregateFluentExtensions.ProjectExpressionProjection2.Render(IBsonSerializer
参数名称:MongoDB.Bson中的MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo)中的成员信息。在MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildProjectedSerializer(ProjectionMapping mapping)中的MongoDB.Bson.Serialization.BsonClassMap.MapMember(MemberInfo memberInfo)位于MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build的MongoDB.Dinver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.Build(Expression node)中的MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.SerializerBuilder.BuildMemberInit(MemberInitExpression节点) (表达式节点,IBsonSerializerRegistry serializerRegistry)MongoDB.Driver.Linq.Translators.AggregateProjectionTranslator.TranslateProject(表达式
1 projector, IBsonSerializer
1参数序列,IBSONSerializerRegistry serializerRegistry)在MongoDB.Driver.IAggregateFluentExtensions.ProjectExpressionProjection2.Render(IBsonSerializer
2.Render(IBsonSerializer
1 documentSerializer, IBsonSerializerRegistry serializerRegistry)2.Render(IBsonSerializer
1 documentSerializer,IBsonSerializerRegistry serializerRegistry)
What is the cause of this? 这是什么原因? Are mapping incorrect or called at a wrong time?
映射不正确或在错误的时间调用?
According to mongodb developers I've contacted with this issue 根据mongodb开发人员的说法,我已经联系过这个问题
Please see this ticket for more info. 有关详细信息,请参阅此票证 。
Improvements will be made to fix this in 2.0.1 and beyond. 将在2.0.1及更高版本中进行改进以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.