How to use multiple primary keys

前端 未结 4 1127
时光说笑
时光说笑 2020-12-06 11:37

I created database, for my android app, witch has 16 tables. I want to use ORMlite mapping. The problem is that I didn\'t find examples where you have composite id(Multiple

4条回答
  •  猫巷女王i
    2020-12-06 12:25

    ServiceStack's OrmLite by design doesn't support multiple composite primary keys. In order for your same POCOs to be useful outside of a db (e.g. NoSQL datastores, cache providers, etc) OrmLite expects a single primary key on each type, which my default is the Id property (overridable with ModelConfig class or [PrimaryKey], [Alias] attributes).

    Work arounds to overcome this limitation include creating a single unique Id but include a unique constraint on the composite primary keys, e.g:

    [CompositeIndex("CompositePkId1","CompositePkId2", Unique = true)] 
    public class Poco 
    {
        [AutoIncrement]
        public int Id { get; set; }
        public int CompositePkId1 { get; set; }
        public int CompositePkId2 { get; set; }
    }
    

    Or creating a pseudo column that contains combination of all primary keys, e.g:

    public class Poco 
    {
        public string Id { get { return CompositePkId1 + ":" + CompositePkId2; } }
        public int CompositePkId1 { get; set; }
        public int CompositePkId2 { get; set; }
    }
    

提交回复
热议问题