Composite Field Identity
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
Composite Identity is a mechanism where the identity of the class is defined by more than one property. These properties define the primary key. Defining composite field identity is the same as defining a single identity:
- Get the property configuration object for the property that you want to be a primary key by calling the HasProperty method.
- Use the IsIdentity method to specify that the property is part of the identity key.
In the following example, the Id and FirstName properties of the Person class are specified as identity keys:
personConfiguration.HasProperty( p => p.Id ).IsIdentity();
personConfiguration.HasProperty( p => p.FirstName ).IsIdentity();
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity()
personConfiguration.HasProperty(Function(p) p.FirstName).IsIdentity()
Composite Identity with KeyGenerator
Telerik Data Access enables you to choose a key generator for one of the identity members. 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.
In the following code snippet, the Id property has a specified key generator Autoinc:
personConfiguration.HasProperty( p => p.Id ).IsIdentity(KeyGenerator.Autoinc);
personConfiguration.HasProperty( p => p.FirstName ).IsIdentity();
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.Autoinc)
personConfiguration.HasProperty(Function(p) p.FirstName).IsIdentity()
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 - Composite 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();
personConfiguration.HasProperty( p => p.FirstName ).IsIdentity();
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()
personConfiguration.HasProperty(Function(p) p.FirstName).IsIdentity()
personConfiguration.FieldNamingRules.AddPrefix = "_"
configurations.Add(personConfiguration)
Return configurations
End Function
End Class