Forum


HomeHomePremiumPremiumDevelopmentDevelopmentDAL2 - Multiple Field Primary KeyDAL2 - Multiple Field Primary Key
Previous
 
Next
New Post
10/10/2013 10:46 AM
 

I am trying to get my objects all connected to the database properly with the DAL2.  One thing that came up was what to do when there is a multiple field primary key.  Specifically I have a table that uses 2 varchar(50) fields to create the primary key.  I would like to use the GetById() function because it automatically incorporates caching but it doesn't appear to take multiple fields for the key. 

What is the best way to retrieve an object using a multiple field primary key?

 
New Post
10/10/2013 11:01 AM
 

Andy,

From what I have read, I am not sure if composite keys have made it into the main branch of PetaPoco, or for that matter, the version of PP used DNN's DAL2.  
But if it is there, you would do it like this:

[PrimaryKey("field1,field2")]

If that doesn't work, then it is not supported yet.  You may have to change the table schema to use an identity as the Primary Key and the make the composite key a unique constraint.  You can still query the unique constraint using the Find().

 
New Post
10/14/2013 9:43 AM
 

I have done some additional research and testing.  It does not appear that composite keys are supported.  Designing things as you suggest appears to be the best way to do things:

  • Add an id field that will be used as the primary key.
  • Add a unique constraint to the table for the multiple fields.

 

Since selects for any table like this will likely need to be based on the unique fields and not the id, caching will have to be handled manually if needed.

 
Previous
 
Next
HomeHomePremiumPremiumDevelopmentDevelopmentDAL2 - Multiple Field Primary KeyDAL2 - Multiple Field Primary Key



Try FREE
30 days money back guaranteed