Mark
Mark

Reputation: 2203

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.

Upvotes: 0

Views: 1720

Answers (2)

labilbe
labilbe

Reputation: 3584

Multiple primary keys don't exist. A multi-column primary key yes. Please take a look on this link https://github.com/ServiceStack/ServiceStack.OrmLite#limitations

As it said

A potential workaround to support tables with multiple primary keys is to create an auto generated Id property that returns a unique value based on all the primary key fields

Upvotes: 1

Mark
Mark

Reputation: 2203

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

Upvotes: 0

Related Questions