【问题标题】:Creating and using a table with a composite primary key in SQLite.Net-PCL在 SQLite.Net-PCL 中创建和使用具有复合主键的表
【发布时间】:2017-08-01 04:17:21
【问题描述】:

众所周知,issue SQLite.Net-PCL 不支持复合 PK,如果我不想退回到类似的结构,我需要这个功能

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

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

表格的布局是这样的

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

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

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

【问题讨论】:

    标签: c# sql sqlite orm


    【解决方案1】:

    您可以使用复合唯一键。

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

    【讨论】:

    • 如果这解决了他们的问题,有人可以发表评论吗?还是更好地标记为已接受?
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2015-03-29
    • 2017-07-22
    相关资源
    最近更新 更多