Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PetaPoco, how to decorate a table that has multi-columns primary keys

Tags:

petapoco

In the example given on PetaPoco's web site, this is how to decorate a class:

[PetaPoco.TableName("articles")]
[PetaPoco.PrimaryKey("article_id")]
public class article
{
    public long article_id { get; set; }
    public string title { get; set; }
    public DateTime date_created { get; set; }
    public bool draft { get; set; }
    public string content { get; set; }
}

But assume that the table articles was modeled to have 2 columns: article_id and title as its primary key (instead of just article_id), then how the decoration in PetaPoco would look like.

like image 455
Kevin Le - Khnle Avatar asked Aug 31 '11 14:08

Kevin Le - Khnle


People also ask

Can we have multiple primary keys on multiple columns in a table?

Each table can only have one primary key. Access can automatically create a primary key field for you when you create a table, or you can specify the fields that you want to use as the primary key.

How can a table have multiple primary keys?

A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary key, they are called a composite key. If a table has a primary key defined on any field(s), then you cannot have two records having the same value of that field(s).

Can 3 columns be a primary key?

For a constraint to be recognized as a primary key, it must contain unique values throughout the row and none of the values must be NULL. In a table, there can only be one primary key. A primary key can have one or as many columns as possible.

Can a table have multiple composite keys?

No. You cannot use more than 1 primary key in the table. for that you have composite key which is combination of multiple fields.


1 Answers

This currently only works in my branch, but you can do this.

[PetaPoco.PrimaryKey("article_id,title")]

My branch can be found here. https://github.com/schotime/PetaPoco

like image 90
Schotime Avatar answered Sep 23 '22 06:09

Schotime