Question

I am working with DAL2 in DNN 7+.

I have a database table with a primary key created from 2-varchar(50) fields.

What is the proper way to declare the multi-field primary key in the attributes of the object?
I typically do:

[PrimaryKey("myField")]

Should I just add a second PrimaryKey attribute?

The second part of the question is how to get the data out of the database.
I would like to use the GetById() function because it incorporates caching but it doesn't appear to support multiple fields. Am I stuck using the Find() method and handling caching manually or what is the best way to select objects that use a multiple field primary keys?

"Look to the sky as much as the screen!"

Was it helpful?

Solution

DAL2 does not support Composite Keys.

The best way I have found to work around this limitation is to modify the database table. I use an Auto-increment int 'id' field for the primary key and set a unique constraint on the fields that I desired to use as the composite key.

This will required the .Find() method to be used which prevents caching. If caching is desired it must be done manually. When cache is manually set, it should be manually cleared as well. If you are using the repository technique make sure to clear the cache when the object is updated, or deleted.

"Look to the sky as much as the screen!"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top