使用mongo C#驱动程序在嵌入式文档中维护Id属性名称
我有一个mongo文档,其中包含一组嵌入文档。嵌入的文档有一个名为“Id”的属性使用mongo C#驱动程序在嵌入式文档中维护Id属性名称,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我有一个mongo文档,其中包含一组嵌入文档。嵌入的文档有一个名为“Id”的属性 BsonClassMap.RegisterClassMap<Inner>(cm => { cm.AutoMap(); cm.UnmapProperty(c => c.Id); cm.MapMember(c => c.Id)
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
我的C#映射对象看起来像这样(显然是一种简化)
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
公共类外部
{
公共字符串名称{get;set;}
公共IEnumerable Inners{get;set;}
}
公共阶级内部
{
公共字符串名称{get;set;}
公共字符串Id{get;set;}
}
当我向数据库中写入外部属性时,C#驱动程序会将内部.Id属性的名称更改为#Id。如何避免这种自动重命名?我尝试在Id属性上使用[BSoneElement(“Id”)]属性,但没有任何帮助。MongoDB文档:
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
MongoDB中的文档需要有一个唯一标识它们的密钥\u id
。
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
另一方面,C#属性通常是帕斯卡大小写,不使用前缀,因此驱动程序设计人员要强制将Id
属性映射到\u Id
数据库属性
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
如果要绑定一个非\u id
属性,而该属性恰好在MongoDB中被调用id
,则可以声明另一个名为id
以外的C属性,这样驱动程序就不会干扰它:
public class Inner
{
public string Name { get; set; }
[BsonElement("Id")]
public string IdStr { get; set; }
}
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
我不能评论,所以我会写新的答案。我的笔记将为人们节省大量时间。 如果mongodb中的_id是ObjectID类型,那么在C中,您需要添加更多属性:
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[BsonElement("Id")]
public string Subject { get; set; }
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
我同意Dan的回答,但在某些情况下,如果不允许将“Id”属性更改为其他属性,则可以在类映射中显式更改行为,如下所示
BsonClassMap.RegisterClassMap<Inner>(cm =>
{
cm.AutoMap();
cm.UnmapProperty(c => c.Id);
cm.MapMember(c => c.Id)
.SetElementName()
.SetOrder(0) //specific to your needs
.SetIsRequired(true); // again specific to your needs
});
BsonClassMap.RegisterClassMap(cm=>
{
cm.AutoMap();
cm.unapproperty(c=>c.Id);
cm.MapMember(c=>c.Id)
.SetElementName()
.SetOrder(0)//特定于您的需要
.SetIsRequired(true);//再次针对您的需要
});
不过,这只是我的猜测。我自己从未使用过MongoDB。我将属性的名称更改为IdStr,并将[BSoneElement(“Id”)]属性添加到属性中。成功了。谢谢谢谢你的回答。它解释了我们今天一直在关注的一个问题。不管你如何覆盖地图。对他们来说,这是多么艰难的一步啊/