Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Single Field Identity
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Object Identity > Single Field Identity

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

OpenAccess ORM provides a mechanism where one exclusive field holds the identity information. The description of this field is done by the [Persistent] attribute. The property IdentityField defines the name of the field used. Only one field can be specified.

Specifying Single field identity

If your class has only one primary key field, it is recommended that you use single field identity to simplify working with identity, since in this case you do not need to code your own identity class.

Use the Metadata Properties dialog to specify the Single Field Identity as shown below:

Single Field should be chosen as the identity type from the drop-down list present alongside the Type label. Next, the user needs to specify the field that will hold the identity information. All the fields with a valid type are available in the drop-down list present alongside the ID Field label. Based on the selected field type, the user can choose the key generator required from a drop-down list present alongside the Key Generator label (refer to db-key-generator for more information):

Key Generator

Supported Types

Comments

HIGHLOW

Int types (SByte, Int16, Int32, Int64)

Cannot be used for strings or Guid as only int values can be generated.

AUTOINC

Int types (SByte, Int16, Int32, Int64)

Cannot be used for single field identity types other than int as the identity column on database cannot be of string type or GUID.

GUID

GUID

If GUID is used for int or string (char, string) types, it is ignored and the HIGHLOW key generator is used instead.

VERIFY

All Single field Identity types

 

Once you select single field identity, the [Persistent(IdentityField="xx")] attribute is added to the class. The IdentityField="xx" specifies the name of the field containing the identity. For e.g.:

Copy Code
[OpenAccess.Persistent(IdentityField="addrid")]
public class Address
{
  private Guid addrid;

  public Guid ID
  {
    get { return addrid; }
    set { addrid = value; }
  }
  // ...

So, in order to use single field identity, all you need to do is specify the field that holds the identity information and OpenAccess ORM manages the rest.

Only one identity mechanism can be specified within a single class hierarchy, i.e., it is not possible to specify both Single Field Identity (IdentityField) and Multiple Field Identity (Identity), within an inheritance hierarchy. Furthermore the fields to be used must be declared in the least-derived class of that hierarchy in case of flat and vertical mapping.

In case of horizontal mapping, the identity information is specified at the next derived class and if the IdentityField specified belongs to the base class, it has to be accessible from the derived classes. Therefore, declaring this field as "Protected" might be the best way, since if this field is declared as "Private" the enhanced code will not be able to access this field. The enhancer will check this and report an error.

Supported Field Types

The Identity fields are visible and the following lists the currently supported field types for use in the single field identity:

 

System.SByte

 

System.Int16

 

System.Int32

 

System.Int64

 

System.Guid

 

System.Char

 

System.String

The key can be set by the application (before calling OpenAccess.Add) or by one of supported key generators (refer to db-key-generator for more information).

If no key generator has been set in the application configuration file, the application is responsible to create new keys before storing data. If you want the runtime to do that, please specify the appropriate key generator in the Metadata Properties dialog.

For e.g., if you have chosen the AUTOINC key generator, in the Metadata Properties dialog, for the address class in our example, the following lines will be added to your app.config file:

Copy Code
<class name = "Address">
  <extension key="db-key-generator" value="AUTOINC">
</class>

If you want to use GUID as the single field identity, it is necessary to have a unique GUID created each time. For this, you will need to initialize the GUID explicitly, by adding ID = Guid.NewGuid(); in the constructor of the ID class.