多个主键 - ORMlite [英] Multiple primary keys - ORMlite

查看:1096
本文介绍了多个主键 - ORMlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的数据库,为我的Andr​​oid应用程序,女巫有16桌。我想用ORMlite映射。问题是,我没有找到,你有复合的ID(多主键)的例子。例如,我有表:

  CREATE TABLE IF NOT EXISTS`Tourist_Guide`.`Cultural_activity`(
  `City_Id` INT NOT NULL,
  `activity_Id` INT NOT NULL,
  `Cultural_activity_Id` INT NOT NULL AUTO_INCREMENT,
  `Name_Of_Cultural_activity` VARCHAR(30)NOT NULL,
  PRIMARY KEY(`Cultural_activity_Id`,`City_Id`,`activity_Id`)
  INDEX`fk_Cultural_activity_activity1`(`City_Id` ASC,`activity_Id` ASC),
  约束`fk_Cultural_activity_activity1`
    外键(`City_Id`,`activity_Id`)
    参考`Tourist_Guide`.`activity`(`City_Id`,`activity_Id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB的;
 

你能不能,请告诉我如何将这些表映射到类(这个类应该是什么样子),是,即使可能吗?

解决方案
  

限制

     

有关简单,并能有db4o中,memcached的,redis的或在文件系统中相同的POCO类持久(即   包括在ServiceStack提供商),每个模型都必须有一个单一的   主键,按照惯例OrmLite希望它是编号虽然你   使用[别名(DbFieldName)]属性它映射到一个列   不同的名称或使用[的PrimaryKey]属性告诉OrmLite到   使用不同属性的主键。

     

您仍然可以从这些表中选择,你只是无法   利用API,它们依赖于它,例如UPDATE或DELETE,其中   过滤器是隐含的(即没有指定),所有与结束的A​​PI   ById等。

     

解决方法单一主键限制

     

一个潜在的解决方法,以支持多个主键的表是   创建生成的ID属性自动返回一个独特的价值   基于所有主键字段,例如:

 公共类的OrderDetail
{
    公共字符串ID {{返回this.OrderId +/+ this.ProductId; }}



公众诠释订单ID {获得;组; }
    公众诠释的ProductId {获得;组; }
    公共小数单价{获得;组; }
    公共短量{获得;组; }
    公共双重优惠{获得;组; }
}
 

https://github.com/ServiceStack/ServiceStack.OrmLite/#limitations

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 primary keys). For example I have table:

CREATE  TABLE IF NOT EXISTS `Tourist_Guide`.`Cultural_activity` (
  `City_Id` INT NOT NULL ,
  `activity_Id` INT NOT NULL ,
  `Cultural_activity_Id` INT NOT NULL AUTO_INCREMENT ,
  `Name_Of_Cultural_activity` VARCHAR(30) NOT NULL ,
  PRIMARY KEY (`Cultural_activity_Id`, `City_Id`, `activity_Id`) ,
  INDEX `fk_Cultural_activity_activity1` (`City_Id` ASC, `activity_Id` ASC) ,
  CONSTRAINT `fk_Cultural_activity_activity1`
    FOREIGN KEY (`City_Id` , `activity_Id` )
    REFERENCES `Tourist_Guide`.`activity` (`City_Id` , `activity_Id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Can you, please, tell me how to map this table to class(how this class should look like), is that even possible?

解决方案

Limitations

For simplicity, and to be able to have the same POCO class persisted in db4o, memcached, redis or on the filesystem (i.e. providers included in ServiceStack), each model must have a single primary key, by convention OrmLite expects it to be Id although you use [Alias("DbFieldName")] attribute it map it to a column with a different name or use the [PrimaryKey] attribute to tell OrmLite to use a different property for the primary key.

You can still SELECT from these tables, you will just be unable to make use of APIs that rely on it, e.g. Update or Delete where the filter is implied (i.e. not specified), all the APIs that end with ById, etc.

Workaround single Primary Key limitation

A potential workaround to support tables with multiple primary keys is to create an auto generated Id property that returns a unique value based on all the primary key fields, e.g:

public class OrderDetail
{
    public string Id { get { return this.OrderId + "/" + this.ProductId; } }



public int OrderId { get; set; }
    public int ProductId { get; set; }
    public decimal UnitPrice { get; set; }
    public short Quantity { get; set; }
    public double Discount { get; set; }
}

https://github.com/ServiceStack/ServiceStack.OrmLite/#limitations

这篇关于多个主键 - ORMlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆