我使用Service Stack将数据存储在带有http-POST的Ormlite数据库中。
我生成以下类来存储我在两个不同的表中收到的数据,我的数据被存储但是我收到错误500。 当我的数据存储成功时,我尝试生成有效的消息(201)。
/// <summary>
/// Define your ServiceStack web service request (i.e. Request DTO).
/// </summary>
public class NewDevice : IReturn<NewDeviceResponse>
{
/// <summary>
/// Gets or sets the id of the NewDevice. The id will be automatically incremented when added.
/// </summary>
[AutoIncrement]
public int Id { get; set; }
// Info fix
public string Type { get; set; }
public string HumanReadableDeviceSN { get; set; }
public string Hardware { get; set; }
public string Model { get; set; }
// Soft
public string Firmware { get; set; }
public string SwVer1 { get; set; }
public string SwVer2 { get; set; }
}
/// <summary>
/// Define your ServiceStack web service response (i.e. Response DTO).
/// </summary>
public class NewDeviceResponse
{
/// <summary>
/// Gets or sets the NewDevice.
/// </summary>
public NewDevice NewDevice { get; set; }
}
/// <summary>
/// Create your ServiceStack restful web service implementation.
/// </summary>
public class NewDeviceService : Service
{
/// <summary>
/// POST /json/reply/NewDevice
/// returns HTTP Response =>
/// 201 Created
/// </summary>
public object Post(NewDevice NewDevice)
{
//Db.Insert(NewDevice);
// ----- Share information posted -----
// ---- DeviceInfo ----
Db.Insert(new DeviceInfo.DeviceInfo { DeviceType = NewDevice.Type, HumanReadDevId = NewDevice.HumanReadableDeviceSN, Hardware = NewDevice.Hardware, Model = NewDevice.Model });
// --- Device History
Db.Insert(new DeviceHistory.DeviceHistory { Firmware = NewDevice.Firmware, SWVer1 = NewDevice.SwVer1, SWVer2 = NewDevice.SwVer2 });
// GET Last insert ID from DeviceInfo and DeviceHistory
var newNewDeviceId = Db.GetLastInsertId();
//var newNewDeviceId = Db.Select<DeviceInfo.DeviceInfo>("SELECT DeviceId FROM DeviceInfo WHERE DeviceType = LAST_INSERT_ID()", DeviceType, HumanReadDevId);
var newNewDevice = new NewDeviceResponse
{
NewDevice = Db.Id<NewDevice>(newNewDeviceId),
};
return new HttpResult(newNewDevice)
{
StatusCode = HttpStatusCode.Created,
Headers =
{
{HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newNewDeviceId.ToString())}
}
};
}
}
这个当前类用于生成一个新表,我修改它只使用http-post来获取数据并在两个表中共享它们。
行(var newNewDeviceId = Db.GetLastInsertId();)获取此前类实现的最后一个插入ID以验证存储(请告诉我,如果我错了)。
但我想得到表格的最后一个插入ID,我存储我的数据(DeviceInfo和DeviceHistory)以验证我的存储。 我尝试用SQL请求来获取它,但它不起作用。
如果您对我的问题有任何建议,请告诉我。
谢谢
答案 0 :(得分:1)
首先确保您的表格有自动递增的主键。
其次,小心多个插入。您应该在每次插入后直接调用GetLastInsertedId 。在SQL的引擎下它转换为
SELECT SCOPE_IDENTITY()
您的代码应该更像以下内容:
Db.Insert(new DeviceInfo.DeviceInfo
{ DeviceType = NewDevice.Type, HumanReadDevId =
NewDevice.HumanReadableDeviceSN,
Hardware = NewDevice.Hardware, Model = NewDevice.Model });
var newNewDeviceId = Db.GetLastInsertId();
Db.Insert(new DeviceHistory.DeviceHistory { Firmware = NewDevice.Firmware,
SWVer1 = NewDevice.SwVer1, SWVer2 = NewDevice.SwVer2 });
var newNewDeviceHistoryId = Db.GetLastInsertId();
// Then in any kind of select use the var not the function
var deviceInfo = Db.SingleById<DeviceInfo.DeviceInfo>(newNewDeviceId);