为什么服务堆栈返回 Int64 而不是 Int32?
Why is service stack returning a Int64 instead of Int32?
我的模型 SecPermission 的列 Id = int 是 Int32。当我添加一条新记录时,为什么它返回新添加的 ID 作为 Int64?
服务方式
public object Post(AddPermission request)
{
var perm = request.ConvertTo<SecPermission>();
perm.AuditUserId = UserAuth.Id;
LogInfo(typeof(SecPermission), request, LogAction.Insert);
return Db.Insert(perm);
}
单元测试代码
using (var service = HostContext.ResolveService<SecurityService>(authenticatedRequest))
{
///**this line is returning an object with Int64 in it.
int id = (int) service.Post(new AddPermission { Name = name, Description = "TestDesc" });
service.Put(new UpdatePermission { Id = permission, Name = name,Description = "TestDesc" });
service.Delete(new DeletePermission { Id = Convert.ToInt32(id)});
}
public class SecPermission : IAudit
{
[AutoIncrement]
[PrimaryKey]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(75)]
public string Description { get; set; }
[Required]
public PermissionType PermissionType { get; set; }
public int AuditUserId { get; set; }
public DateTime AuditDate { get; set; } = DateTime.Now;
}
你永远不应该 return ServiceStack 服务中的值类型,它需要是引用类型,通常是类型化响应 DTO,但也可以是原始数据类型,如 string
或 byte[]
但它不应该是像整数这样的值类型,它在某些 ServiceStack 功能中无法工作。
对于此服务,我将 return SecPermission
对象或结果值中包含整数的 AddPermissionResponse
对象。
请注意 OrmLite Insert()
API return 是一个 long
这就是为什么你会看到很长的原因,但是你需要 call Save() or specify selectIdentity:true为了获取新插入的 [AutoIncrement]
主键的 ID,例如:
var newId = db.Insert(perm, selectIdentity:true);
或
Db.Save(perm);
var newId = perm.Id; //auto populated with auto incremented primary key
此外,您不需要在 OrmLite 中同时使用 [PrimaryKey]
和 [AutoIncrement]
,因为 [AutoIncrement]
会像使用 Id property convention 一样自行指定主键。
此外,如果您要直接调用该服务,您也可以键入响应以避免转换,例如:
public SecPermission Post(AddPermission request)
{
//...
Db.Save(perm);
return perm;
}
那么直接调用就不用cast了,例如:
var id = service.Post(new AddPermission { ... }).Id;
在 ServiceStack 中使用 object
或像 SecPermission
这样的类型化响应没有行为差异,尽管最好使用 IReturn<T>
接口标记在您的 Request DTO 上指定它,例如:
public AddPermission : IReturn<SecPermission> { ... }
因为它在从服务客户端调用时启用端到端类型化 APIs,例如:
SecPermission response = client.Post(new AddPermission { ... });
我的模型 SecPermission 的列 Id = int 是 Int32。当我添加一条新记录时,为什么它返回新添加的 ID 作为 Int64?
服务方式
public object Post(AddPermission request)
{
var perm = request.ConvertTo<SecPermission>();
perm.AuditUserId = UserAuth.Id;
LogInfo(typeof(SecPermission), request, LogAction.Insert);
return Db.Insert(perm);
}
单元测试代码
using (var service = HostContext.ResolveService<SecurityService>(authenticatedRequest))
{
///**this line is returning an object with Int64 in it.
int id = (int) service.Post(new AddPermission { Name = name, Description = "TestDesc" });
service.Put(new UpdatePermission { Id = permission, Name = name,Description = "TestDesc" });
service.Delete(new DeletePermission { Id = Convert.ToInt32(id)});
}
public class SecPermission : IAudit
{
[AutoIncrement]
[PrimaryKey]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(75)]
public string Description { get; set; }
[Required]
public PermissionType PermissionType { get; set; }
public int AuditUserId { get; set; }
public DateTime AuditDate { get; set; } = DateTime.Now;
}
你永远不应该 return ServiceStack 服务中的值类型,它需要是引用类型,通常是类型化响应 DTO,但也可以是原始数据类型,如 string
或 byte[]
但它不应该是像整数这样的值类型,它在某些 ServiceStack 功能中无法工作。
对于此服务,我将 return SecPermission
对象或结果值中包含整数的 AddPermissionResponse
对象。
请注意 OrmLite Insert()
API return 是一个 long
这就是为什么你会看到很长的原因,但是你需要 call Save() or specify selectIdentity:true为了获取新插入的 [AutoIncrement]
主键的 ID,例如:
var newId = db.Insert(perm, selectIdentity:true);
或
Db.Save(perm);
var newId = perm.Id; //auto populated with auto incremented primary key
此外,您不需要在 OrmLite 中同时使用 [PrimaryKey]
和 [AutoIncrement]
,因为 [AutoIncrement]
会像使用 Id property convention 一样自行指定主键。
此外,如果您要直接调用该服务,您也可以键入响应以避免转换,例如:
public SecPermission Post(AddPermission request)
{
//...
Db.Save(perm);
return perm;
}
那么直接调用就不用cast了,例如:
var id = service.Post(new AddPermission { ... }).Id;
在 ServiceStack 中使用 object
或像 SecPermission
这样的类型化响应没有行为差异,尽管最好使用 IReturn<T>
接口标记在您的 Request DTO 上指定它,例如:
public AddPermission : IReturn<SecPermission> { ... }
因为它在从服务客户端调用时启用端到端类型化 APIs,例如:
SecPermission response = client.Post(new AddPermission { ... });