Skip to content

Composite Primary Keys

Bryan Rayner edited this page Jul 12, 2016 · 2 revisions

Composite keys can be specified by placing a comma between the two column names in the [PrimaryKey] attribute.

[TableName("Users")]
[PrimaryKey("UserId,UserName")]
public class User
{
    public int UserId { get; set; }
    public string UserName { get;set; }
}

When setting a composite key, the AutoIncrement attribute will always default false.

If you want to get one of these objects from the database using the SingleById group of methods then you can use an anonymous type.

IDatabase db = new Database("connStringName");
var user = db.SingleById<User>(new {UserId = 1, UserName = "user"});