问题描述
我有一个数据库 [![as this picture][1]][1]
我想从这个数据库中获取一个图表,当我尝试在控制器端使用 sql 查询时,出现了这样的错误;
[![这是错误类型][2]][2]
这是我的控制器代码;
public ActionResult Index2(String makineadi,DateTime? startdate,DateTime? enddate)
{
var data = entities.Database.sqlQuery<DataPoint>("SELECT Sıcaklık,Recete_Sure From Recete Where Machine_IP ='" + makineadi + "' and Tarih between'"+startdate+"' and '"+enddate+"'").ToList();
ViewBag.DataPoints = JsonConvert.SerializeObject(data);
return View();
}
这是我的类定义,用于将图表转换为 JSON 序列化;
[DataContract]
public class DataPoint
{
public DataPoint(int Sıcaklık,int Recete_Sure)
{
this.Sıcaklık = Sıcaklık;
this.Recete_Sure = Recete_Sure;
}
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "Sıcaklık")]
public Nullable<int> Sıcaklık { get; set; }
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "Recete_Sure")]
public Nullable<int> Recete_Sure { get; set; }
}
我该怎么做才能修复它? [1]:https://i.stack.imgur.com/ObwWR.png [2]:https://i.stack.imgur.com/zQNgO.png
解决方法
在类中添加默认构造函数。
[DataContract]
public class DataPoint
{
public DataPoint()
{
}
public DataPoint(int Sıcaklık,int Recete_Sure)
{
this.Sıcaklık = Sıcaklık;
this.Recete_Sure = Recete_Sure;
}
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "Sıcaklık")]
public Nullable<int> Sıcaklık { get; set; }
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "Recete_Sure")]
public Nullable<int> Recete_Sure { get; set; }
}
它会解决问题