本文介绍了如何使用10gen C#官方驱动程序设置地理值的序列化选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此类:

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异常13026地理值必须为数字: {Lat:"50.0853779",Long:"19.931276700000012"} 1ms .为了缓解这个问题,我设置了这样的地图:

I have a geospatial index on the collection set like { Geo: "2d" }. Unfortunately the driver tries to store lat/lon coordinates as strings, instead of numbers and I get an error that says Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo values have to be numbers: { Lat: "50.0853779", Long: "19.931276700000012" } 1ms. To alleviate this problem I setup a map like this:

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,这似乎是合乎逻辑的.我有什么选择?

Notice that there is no BsonType.Decimal nor anything like that. In the effect, when trying to call Save() I get a MongoDB.Bson.TruncationException, which seems logical. What are my options?

推荐答案

根据此错误(已于2011年1月21日UTC修复),在c#官方驱动程序中添加了功能"AllowTruncation".因此,您需要下载最新的驱动程序版本并享受!另外,也可以像这样使用BsonRepresentationAttribute代替SetRepresentation:

According this bug(fixed Jan 21 2011 05:46:23 AM UTC), in c# official driver was added ability 'AllowTruncation'. So you need download latest driver version and enjoy! Also instead of SetRepresentation you can use BsonRepresentationAttribute like this:

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

这篇关于如何使用10gen C#官方驱动程序设置地理值的序列化选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:56