我正在使用Dapper而不是Entity Framework。尝试使用嵌套时遇到错误对象作为查询参数。
错误看起来像这样:
App.Model.Device类型的Device的成员不能用作参数值。
[如果有任何我可以改善的问题结构,请发表评论。不要猛击。谢谢
我有一些看起来像这样的类:
public class RootObject
{
public List<Datum> data { get; set; }
}
public class Datum
{
public Device device { get; set; }
public long time { get; set; }
public string data { get; set; }
}
public class Device
{
public string id { get; set; }
}
和一个SQL插入查询,看起来像这样:
private const string InsertQuery = @"Insert into iot.[Table] (DeviceID,DeviceData,UnixTime) values(@device.id,@data,@time)";
*更新后的答案
var ro = RootObject.Init(cnnStr, commandTimeout: 100);
Device dev = new Device{id="123456"}
long unixtime = long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1,> 1))).TotalSeconds;
Datum dat = new Datum{device=dev,time=unixtime,data="abcdef"};
dat.id = ro.Datum.Insert(dat);
或
string insertQuery = @"Insert into iot.[Table] (DeviceID,DeviceData,UnixTime) values(@id,@data,@time)";
var result = db.Execute(insertQuery, new
{
Device.ID,
strData,
(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds
});