Internal Identity
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. The internal identity (or High-Low) mechanism relies that a certain table (voa_keygen) is presented in your database. The internal identity field has an integer type and is calculated by the Telerik Data Access High-Low key generator. The voa_keygen table will be used for storage of the internal identities. Generally, the voa_keygen table will be used to connect the identity column of the table with the column value.
To configure internal identity with Fluent Mapping, you need to specify that the property that is part of the identity key uses the HighLow key generator. The following example demonstrates how to configure internal identity for the Person class.
personConfiguration.HasProperty( p => p.Id ).IsIdentity(KeyGenerator.HighLow);
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.HighLow)
In the previous example, it is supposed that the Person class has an integer property named Id that will be used as an internal identity.
However, if there isn't a specific property/column that could be used as an identity, Telerik Data Access will automatically create one:
personConfiguration.HasIdentity(KeyGenerator.HighLow);
personConfiguration.HasIdentity(KeyGenerator.HighLow)
When you update your database schema to the latest model state, Telerik Data Access will automatically create a new column ("TableName_Id"), which will be used as an internal primary key. The new column is of type SQL INTEGER.
Finally, you can explicitly specify the name for the internal identity column by using the ToColumn method:
personConfiguration.HasIdentity(KeyGenerator.HighLow).ToColumn("InternalIdentityColumn");
personConfiguration.HasIdentity(KeyGenerator.HighLow).ToColumn("InternalIdentityColumn")
For more information about the Telerik Data Access internal identity mechanism, see Working with HighLow Keygen API.
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 - Internal 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.HighLow);
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.HighLow)
personConfiguration.FieldNamingRules.AddPrefix = "_"
configurations.Add(personConfiguration)
Return configurations
End Function
End Class