help.axcms.netAxinom Logo
Save Save Chapter Send Feedback

Primary Key Reservation in AxCMS.net

The AxCMS.neet Persistence Framework provides a default mechanism for handling and reserving primary keys for new objects. This mechanism is based on AxCMS.net Sequence. You can override it and provide your own implementation.

The AxCMS.net Persistence Framework provides a default mechanism for handling and reserving primary keys for new objects. This mechanism is based on CMS Sequence. You can override it and provide your own implementation (see Complex Primary Keys).

What is AxCMS.net Sequence?

CMS Sequence is a reusable way to manage integer primary keys. It is based on the table Sequence, which is present both in MS and in LS databases. Every record of this table represents a counter, which can be used for primary keys of one table. Don't access and increment counters in the table directly. Please use the class SqlSequenceManager instead.

How can I add a counter for my BL table?

Use the following method:

SqlSequenceManager.Current.CreateSequence(string name,
                                          long nextVal,
                                          long minVal,
                                          long maxVal,
                                          int cache,
                                          int cycle,
                                          int incrementBy); 

For example:

SqlSequenceManager.Current.CreateSequence("Article", 1, 1, 2147483646, 1, 0, 1); 

How can I reserve a new unused id?

long newValue = SqlSequenceManager.Current.NextVal("Article"); 

How can I use automatic primary key reservation?

To use automatic primary key reservation you have to comply with the following rules:

1) One and only one property of your BL class must be mapped as the primary key:

//Adapter pattern
public override AxDbFieldMapping[] Mappings
{
   get
        {
         return new AxDbFieldMapping[]
                {
                 ...
                 AxDbFieldMapping("Id", "ArticleID", true);
                 ...
                };
        }
}

or

//Generic adapter or DbObject pattern
[AxDbPrimaryKey]
[AxDbMapToField]
public int ID {...} 

2) The property must be of type int.

3) The Sequence name should be defined:

//Adapter pattern
protected override string KeySequenceName
{
   get { return "Article"; }
}

or

//Generic adapter or DbObject pattern
[AxDbMapToTable("Article", "A", "Article")]
class Article: ...

4) Initial value of your primary key property should be less than 1 to indicate the need for automatic primary key reservation.