简体   繁体   中英

Service Stack ormlite generate http response for tables linked

I use Service Stack to store my data with an Ormlite database with an http-POST.

I generate the below class to store my data received in two different tables, my data are stored but I get an error 500. I try to generate an valid message (201) when the storage of my data is successful.

/// <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())}
                           }
        };
    }
}

This current class was used to generate a new table, I modified it to use only the http-post to get data and share them in two tables.

The row (var newNewDeviceId = Db.GetLastInsertId();) get the last insert ID of this former class implementation to validate the storage(Please, Tell me if I'm wrong).

But I want to get the last insert Id of the table where I store my data (DeviceInfo and DeviceHistory) to validate my storage. I try to get it with an SQL request, but it don't work.

Please if you have any suggestion for my problem let me know.

Thanks

First make sure your table has an auto incremented primary key .

Second, Be careful with multiple inserts. You should call GetLastInsertedId directly after each insert . Under the hood in SQL it translates into

SELECT SCOPE_IDENTITY()

Your code should look more like the following:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM