ServiceStack 如何仅使用主键生成 Json 响应? [英] ServiceStack How generate an Json response with only the Primary Key?
问题描述
当我在我的表中创建一个新记录时,我想生成一个只有我的新记录的主 ID 的 json 响应,比如:{"PrimaryID":123}
When I create a new record in my table I would like generate an json response with only the primary ID of my new record, somethink like : {"PrimaryID":123}
我实际上使用了这个手工制作的功能:
I actually use this handmade function:
// Inserts a new row into the PatientSession table
public string AddPatientSession(PatientSession p)
{
int id = (int)_dbConnection.Insert<PatientSession>(p, selectIdentity: true);
string Idconvert = id.ToString();
string IdInsert = "{\"PatientSessionId\":" + Idconvert + "}";
return IdInsert;
}
但我认为这不是最好的方法,请问您有什么建议吗?提前致谢
But I assume it's not the best way to do it, have you a suggestion please? Thanks in advance
推荐答案
非常感谢@mythz,它运行良好,我只是使用了一个转换为 int 的函数,因为Db.Insert"返回一个 long 类型.
Thanks you so much @mythz it's working well I just use a convert function to int because "Db.Insert" return a long type.
// Add PatientSession via POST
public class PatientSessionADD : IReturn<PatientSessionResponseId>
{
public int PatientSessionId { get; set; }
public int ByPatientId { get; set; }
public DateTime PatientStartSessionTime { get; set; }
public int PatientStartSessionByUserId { get; set; }
public DateTime PatientEndSessionTime { get; set; }
public int PatientEndSessionByUserId { get; set; }
}
public class PatientSessionResponseId
{
public int PatientSessionId { get; set; }
}
public object Post(PatientSessionADD request)
{
var p =new PatientSession()
{
ByPatientId = request.ByPatientId,
PatientStartSessionTime = request.PatientStartSessionTime,
PatientStartSessionByUserId = request.PatientStartSessionByUserId
};
return new PatientSessionResponseId
{
PatientSessionID = Convert.ToInt16( Db.Insert<PatientSession>(p, selectIdentity: true) )
};
}
要恢复此功能,请获取 HTTP POST 消息,将其存储在数据库中并返回仅生成主 ID 的 JSON 响应.
To resume this function get a HTTP POST message, store it in database and return a JSON response with only the Primary ID generated.
玩得开心,再次感谢 mythz
Have fun and thanks again mythz
这篇关于ServiceStack 如何仅使用主键生成 Json 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!