How can I use OrmLite with composite primary keys?

5,305 views
Skip to first unread message

Bastien

unread,
Jul 28, 2012, 10:54:07 AM7/28/12
to servic...@googlegroups.com
Is it somehow possible to use OrmLite with multiple primary keys?

About the usage... this is just a general question not bound to any certain sql statement.

I have tables with an integer value as PK and a DateTime as PK. I can not add a 3rd column as Identity Column because 

I am inserting historized data where I need full control about the integer manually increasing etc...

Demis Bellot

unread,
Jul 28, 2012, 12:11:50 PM7/28/12
to servic...@googlegroups.com
Hi Bastien,

Under Limitations on the OrmLite page I've described this limitation and potential workaround:

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


Thanks,
--
- Demis


Reply all
Reply to author
Forward
0 new messages