Search code examples
servicestackormlite-servicestack

ServiceStack OrmLite - database first & multiple primary keys


I have to work off an existing Db & would like to use ServiceStack's OrmLite.

Thus I have created Poco classes, using OrmLite T4 templates.

ISSUE: I would like to save to a table which has multiple primary keys.

 public partial class DbUserGroup
{
    [Required]
    public int Userid { get; set;} // this is a primary key
    [Required]
    public int Groupid { get; set;} // this is a primary key

    public int Ranking { get; set;}
    public bool Isprimary { get; set;}
}

Currently using Db.Save(userGroup) does not work. Is there any way of addressing this using ServiceStack's OrmLite.


Solution

  • I resolved it by adding [PrimaryKey] to both properties.

    public partial class DbUserGroup
    {
        [Required]
        [PrimaryKey]
        public int Userid { get; set;} // this is a primary key
    
        [Required]
        [PrimaryKey]
         public int Groupid { get; set;} // this is a primary key
    
        public int Ranking { get; set;}
        public bool Isprimary { get; set;}
    }