Skip to main content

KeyAttribute Class

Specifies one or more table columns that make up a primary key for an entity class.

Declaration

KeyAttribute = class(
    TCustomAttribute
)

Remarks

Primary key values uniquely identify entity objects (instances of entity classes) and records in a mapped data store table – no more than one entity object can refer to the same table record.

Options to apply the KeyAttribute include:

  • A field or a read-write property. Map this field/property to a primary key column using the ColumnAttribute or AutomappingAttribute. Applying the KeyAttribute to a field allows you to expose this field via a read-only property, thus prohibiting any changes to its key value using the property. The following code shows how to accomplish this.
uses
  ..., dxEMF.Core, dxEMF.Attributes;
type
  [Entity]
  TPerson = class
  strict private
    [Column, Key]
    FId: Integer;
    // ...
  public
    property Id: Integer read FId;
    // ...
  end;
  • An entity class. Pass the name of a column as the KeyAttribute‘s parameter to designate this column as a primary key. To specify a composite primary key, pass two or more column names delimited by a comma or a semicolon as shown below. Alternatively, you can apply multiple KeyAttributes specifying each column as their parameter.
type
  [Entity]
  [Key('ID,CourseID')]
  TPerson = class
  strict private
    FId: Integer;
    FCourseID: Integer;
    // ...
  public
    [Column]
    property Id: Integer read FId;
    [Column]
    property CourseID: Integer read FCourseID write FCourseID;
    // ...
  end;

Do one of the following to provide a unique key value:

  • Manually assign a unique value to the key field in the class’s constructor. Saving a newly created entity object stores this value to the mapped key column.

  • For a simple primary key, mark the key field/property with the GeneratorAttribute to allow the data store to automatically generate values for the key column mapped to this field/property when inserting a new table record.

Inheritance

See Also