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

如何使用官方的10gen C#驱动程序设置geo值的序列化选项?,c#,serialization,mongodb,mongodb-.net-driver,C#,Serialization,Mongodb,Mongodb .net Driver,考虑到这一类: public class Location { public Coordinates Geo { get; set; } public Location() { Geo = new Coordinates(); } public class Coordinates { public decimal Lat { get; set; } public decimal Long { ge

考虑到这一类:

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”,长:“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);
});
public class C {
  [BsonRepresentation(BsonType.Double, AllowTruncation=true)]
  public decimal D;
}
BsonClassMap.RegisterClassMap(cm=>
{
cm.AutoMap();
MapProperty(c=>c.Lat).SetRepresentation(BsonType.Double);
MapProperty(c=>c.Long).SetRepresentation(BsonType.Double);
});
请注意,这里没有
BsonType.Decimal
或类似的内容。实际上,当尝试调用
Save()
时,我得到了一个
MongoDB.Bson.TruncationException
,这似乎是合乎逻辑的。我的选择是什么?

根据这一点,在c#中,官方驱动程序添加了“允许运行”功能。因此,您需要下载最新的驱动程序版本并享受!也可以使用BsonRepresentationAttribute代替SetRepresentation,如下所示:

BsonClassMap.RegisterClassMap<Location.Coordinates>(cm =>
{
    cm.AutoMap();
    cm.MapProperty(c => c.Lat).SetRepresentation(BsonType.Double);
    cm.MapProperty(c => c.Long).SetRepresentation(BsonType.Double);
});
public class C {
  [BsonRepresentation(BsonType.Double, AllowTruncation=true)]
  public decimal D;
}

是的,我知道我可以使用属性,我只是选择不使用,我不想在我的域模型中有更多的依赖项。@Pawel:So AllowRunLocation=true work in new version?因为我实际上没有测试,我也想知道。是的,我在
SetRepresentation()
中找不到它(不知道为什么没有放在那里…),而是必须通过
SetSerializationOptions(new-RepresentationSerializationOptions(BsonType.Double,false,true))
来设置它。当然,还是属性。我不想降低精度,但看看地图,我想这“足够好了”。