gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:43:49 27 4
gpt4 key购买 nike

我有以下存储在 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();

我收到以下异常:

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 及更高版本中进行改进以解决此问题。

关于c# - mongodb c# 驱动程序 - 继承、映射和序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670319/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com