考虑这个类:
public class Location
{
public Coordinates Geo { get; set; }
public Location()
{
Geo = new Coordinates();
}
public class Coordinates
{
public decimal Lat { get; set; }
public decimal Long { get; set; }
}
}
我在集合集上有一个地理空间索引,例如{ Geo: "2d" }
。不幸的是,驱动程序尝试将纬度/经度坐标存储为字符串,而不是数字,我收到一条错误消息3 月 15 日星期二 16:29:22 [conn8] 插入数据库.位置异常 13026 地理值必须是数字:{ Lat: "50.0853779", Long: "19.931276700000012" } 1ms。为了缓解这个问题,我设置了这样的地图:
BsonClassMap.RegisterClassMap<Location.Coordinates>(cm =>
{
cm.AutoMap();
cm.MapProperty(c => c.Lat).SetRepresentation(BsonType.Double);
cm.MapProperty(c => c.Long).SetRepresentation(BsonType.Double);
});
请注意,没有BsonType.Decimal
也没有类似的事情。实际上,当尝试调用时Save()
我得到一个MongoDB.Bson.TruncationException
,这似乎合乎逻辑。我有什么选择?
根据这个错误(已于 2011 年 1 月 21 日上午 05:46:23 UTC 修复) https://jira.mongodb.org/browse/CSHARP-113?page=com.atlassian.jira.plugin.system.issuetabpanels%3aall-tabpanel,在 C# 官方驱动程序中添加了“AllowTruncation”功能。因此,您需要下载最新的驱动程序版本并享受!您还可以使用 BsonRepresentationAttribute 代替 SetRepresentation,如下所示:
public class C {
[BsonRepresentation(BsonType.Double, AllowTruncation=true)]
public decimal D;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)