FallenWarrior
FallenWarrior

Reputation: 686

Creating and using a table with a composite primary key in SQLite.Net-PCL

It is a known issue that SQLite.Net-PCL does not support composite PKs, a feature I need in my case if I do not want to fall back to constructs like

create table something(value varchar primary key not null);
insert into something(value) values("$Value1,$Value2");

Manually (without using ORM) creating a table with a composite primary key also does not work, throwing the same SQLiteException, telling me that my table has multiple primary keys.

The table's layout is like this

class ChannelBinding {
    [PrimaryKey]
    public int Id { get; set; }

    [PrimaryKey]
    public string ChannelId { get; set; }
}

and I was wondering if there is any known workaround that would be able to emulate the behaviour of a composite PK.

Upvotes: 6

Views: 2677

Answers (1)

Alan Nunes
Alan Nunes

Reputation: 323

You can use a composite unique key.

class ChannelBinding {
    [Indexed(Name = "CompositeKey", Order = 1, Unique = true)]
    public int Id { get; set; }

    [Indexed(Name = "CompositeKey", Order = 2, Unique = true)]
    public string ChannelId { get; set; }
}

Upvotes: 5

Related Questions