在 SQLite.Net-PCL 中创建和使用具有复合主键的表

Fal*_*ior 5 c# sql sqlite orm

SQLite.Net-PCL 不支持复合 PK是一个已知问题,如果我不想退回到类似的构造,那么在我的情况下我需要这个功能

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

手动(不使用 ORM)创建具有复合主键的表也不起作用,抛出相同的SQLiteException,告诉我我的表有多个主键。

桌子的布局是这样的

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

    [PrimaryKey]
    public string ChannelId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何已知的解决方法能够模拟复合 PK 的行为。

Ala*_*nes 5

您可以使用复合唯一键。

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; }
}
Run Code Online (Sandbox Code Playgroud)