gpt4 book ai didi

c# - 如何使用官方 10gen C# 驱动程序设置地理值的序列化选项?

转载 作者:可可西里 更新时间:2023-11-01 09:12:40 25 4
gpt4 key购买 nike

考虑这个类:

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"} 。不幸的是,驱动程序试图将纬度/经度坐标存储为字符串,而不是数字,我收到一条错误消息:Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo values have to be numbers: { 纬度:“50.0853779”,经度:“19.931276700000012”} 1 毫秒。为了缓解这个问题,我设置了一个这样的 map :

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,这似乎是合乎逻辑的。我有哪些选择?

最佳答案

根据这个bug(fixed Jan 21 2011 05:46:23 AM UTC) , 在c#官方驱动中增加了能力'AllowTruncation'。所以你需要下载最新的驱动程序版本并享受!您也可以像这样使用 BsonRepresentationAttribute 而不是 SetRepresentation:

public class C {
[BsonRepresentation(BsonType.Double, AllowTruncation=true)]
public decimal D;
}

关于c# - 如何使用官方 10gen C# 驱动程序设置地理值的序列化选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314238/

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