C# 在c中运行web服务时mongodb数据丢失#

C# 在c中运行web服务时mongodb数据丢失#,c#,linq,web-services,mongodb,C#,Linq,Web Services,Mongodb,我有一个完整的班级 public class City { [BsonId] public string id { get; set; } public int value { get; set; } } 我创建了一个web服务,其中有一个webmethod [WebMethod] public List<City> linQuery() { MongoConn dao = new MongoConn();

我有一个完整的班级

public class City
{
    [BsonId]
    public string id { get; set; }
    public int value { get; set; }        
}
我创建了一个web服务,其中有一个webmethod

[WebMethod]
    public List<City> linQuery()
    {
        MongoConn dao = new MongoConn();
        MongoServer mongo = dao.getConnection();
        List<City> list = new List<City>();

        string dbName = dao.dbName();
        var db = mongo.GetDatabase(dbName);

        Console.WriteLine(db);
        using (mongo.RequestStart(db))
        {
            var collection = db.GetCollection<City>("cityMap");
            IQueryable<City> cities = collection.AsQueryable().Where(c => c.value > 1200);
            foreach (City city in cities)
                list.Add(city);
            return list;
        }
    }
我怎样才能解决这个问题?
谢谢你的帮助。

你在
City
课程中尝试过为你的属性设置
allowtrunation=true

public class City
{
    [BsonId]
    [BsonRepresentation(BsonType.Int, AllowTruncation=true)]
    public string id { get; set; }
    [BsonRepresentation(BsonType.Int, AllowTruncation=true)]
    public int value { get; set; }        
}

您通常不希望模型知道或关心它的存储方式,因此在模型中添加
MongoDB.Bson
属性并不理想。您可以使用此配置配置MongoDB.Driver读取和存储小数的方式

BsonSerializer.RegisterSerializer(
  typeof(decimal),
   new DecimalSerializer(BsonType.Double,
   new RepresentationConverter(
     true, //allow truncation
     true // allow overflow, return decimal.MinValue or decimal.MaxValue instead
    ))
 );

将字段public int value{get;set;}更改为public double value{get;set;}@ToanNguyen谢谢您的回答。我改变了属性和服务的类型,不抛出异常。它起作用了,我用它来绕过一个错误的模型,该模型使用了双精度而不是小数,将它添加到处理器中,允许它在不转换旧数据的情况下继续,至少在我们不再需要数据之前,这是一个很好的解决方案,这样的代码放在哪里?它看起来是静态的,但进入启动文件这样的东西是有意义的,对吗?假设它在应用程序的生命周期中是一个全球性的东西。Alec是的,它是全球性的。我倾向于在存储库中使用静态构造函数,因为它将所有MongoDB内容保存在一个地方,但如果该类是泛型的,我会选择Startup。
BsonSerializer.RegisterSerializer(
  typeof(decimal),
   new DecimalSerializer(BsonType.Double,
   new RepresentationConverter(
     true, //allow truncation
     true // allow overflow, return decimal.MinValue or decimal.MaxValue instead
    ))
 );