1

My model SecPermission has the column Id = int which is Int32. When I add a new record why is it returning the newly added ID as Int64?

Service method

public object Post(AddPermission request)
        {
            var perm = request.ConvertTo<SecPermission>();
            perm.AuditUserId = UserAuth.Id;
            LogInfo(typeof(SecPermission), request, LogAction.Insert);
            return Db.Insert(perm);
        }

Unit Test code

     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;
        }
Steve Coleman
  • 1,987
  • 2
  • 17
  • 28

1 Answers1

1

You should never return a Value Type in a ServiceStack Service, it needs to be a reference Type, typically a Typed Response DTO but can also be a raw data type like string or byte[] but it should never be a Value Type like an integer which will fail to work in some ServiceStack features.

For this Service I'd either return the SecPermission object or a AddPermissionResponse object with the integer in the result value.

Please note that OrmLite Insert() API returns a long which is why you're seeing a long, however you need to either call Save() or specify selectIdentity:true in order to fetch the newly inserted id of an [AutoIncrement] primary key, e.g:

var newId = db.Insert(perm, selectIdentity:true);

or

Db.Save(perm);
var newId = perm.Id; //auto populated with auto incremented primary key

Also you don't need both [PrimaryKey] and [AutoIncrement] in OrmLite as [AutoIncrement] specifies a Primary Key on its own as does using an Id property convention.

Also if you're going to call the Service directly you may as well Type the Response to avoid casting, e.g:

public SecPermission Post(AddPermission request)
{
    //...
    Db.Save(perm);
    return perm;
}

Then you don't need to cast when calling it directly, e.g:

var id = service.Post(new AddPermission { ... }).Id;

There's no behavioral difference in ServiceStack for using object or a typed response like SecPermission although it's preferably to specify it on your Request DTO using the IReturn<T> interface marker, e.g:

public AddPermission : IReturn<SecPermission> { ... }

As it enables end-to-end Typed APIs when called from Service Clients, e.g:

SecPermission response = client.Post(new AddPermission { ... });
mythz
  • 141,670
  • 29
  • 246
  • 390