Data Access has been discontinued. Please refer to this page for more information.

Single Field Identity

Defining a single field identity is a straight-forward operation. You should perform two steps:

  1. Get the property configuration object for the property that you want to be a primary key by calling the HasProperty method.
  2. Use the IsIdentity method to specify that the property is part of the identity key.

In the following example, the Id property of the Person class is specified as an identity key:

personConfiguration.HasProperty( p => p.Id ).IsIdentity();
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity()

If there isn't a specific property that could be used as a primary key, Telerik Data Access will automatically create a primary key column in the database.

Specifying KeyGenerator

The Telerik.OpenAccess.Metadata.KeyGenerator enumeration specifies the identity mechanism (key generator) for the current class. The following scenarios are possible:

  • Default - specifies that the default value for the key generator will be used by the runtime.
  • AutoInc - defines the 'AUTOINC' key generator.
  • Guid - defines the 'Guid' key generator. Your identity field will require Guid values.
  • HighLow - defines the 'HighLow' key generator. Internal identity will be used. Telerik Data Access always needs to associate its persistent types with a primary key. If such is not supplied, Telerik Data Access uses internal mechanism for creating IDs. This mechanism relies that a certain table (voa_keygen) is presented in your database. For more information, see Internal Identity.
personConfiguration.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity( KeyGenerator.Autoinc )

Person Class

public class Person
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public string Address {get;set;}
}
Public Class Person
    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property

    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property

    Private _address As String
    Public Property Address() As String
        Get
            Return _address
        End Get
        Set(ByVal value As String)
            _address = value
        End Set
    End Property
End Class

Sample FluentMetadataSource Implementation - Single Identity

public class FluentModelMetadataSource : FluentMetadataSource
{
   protected override IList<MappingConfiguration> PrepareMapping()
   {
       List<MappingConfiguration> configurations = new List<MappingConfiguration>();
       MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();

       personConfiguration.MapType( p => new
       {
           Id = p.Id,
           FirstName = p.FirstName,
           LastName = p.LastName,
           HomeAddress = p.Address
       } ).ToTable( "People" );

       personConfiguration.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );

       configurations.Add( personConfiguration );
       return configurations;
   }
}
Public Class FluentModelMetadataSource
    Inherits FluentMetadataSource
    Protected Overrides Function PrepareMapping() As _
        System.Collections.Generic.IList(Of Telerik.OpenAccess.Metadata.Fluent.MappingConfiguration)
        Dim configurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()

        Dim personConfiguration As New MappingConfiguration(Of Person)()
        personConfiguration.MapType(Function(p) New With {Key .Id = p.Id,
                                          Key .FirstName = p.FirstName,
                                          Key .LastName = p.LastName,
                                          Key .HomeAddress = p.Address}).ToTable("People")

        personConfiguration.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.Autoinc)

        personConfiguration.FieldNamingRules.AddPrefix = "_"
        configurations.Add(personConfiguration)
        Return configurations
    End Function
End Class