High-Low Identity Mechanism
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.
This topic provides an example of how to work with the High-Low identity mechanism.High-Low identity means that 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.
To complete this tutorial, you must have a basic understanding of Model First Mapping.
Consider the following example. Suppose, you are doing a Model First Mapping, and you have an entity named Product.
The Product entity does not have an explicitly specified primary key property. In this case the Identity Mechanism property for the Product entity is ignored. When you create your database by using the Update Database from Model Wizard, the result script will be:
CREATE TABLE [product] (
[product_id] INT NOT NULL, --<internal-pk>
[nme] varchar(255) NULL, -- _name
CONSTRAINT [pk_product] PRIMARY KEY ([product_id])
)
GO
-- OpenAccessRuntime.Relational.sql.HighLowRelationalKeyGenerator
CREATE TABLE [voa_keygen] (
[TABLE_NAME] varchar(64) NOT NULL,
[last_used_id] INT NOT NULL,
CONSTRAINT [pk_voa_keygen] PRIMARY KEY ([TABLE_NAME])
)
GO
Few things should be pointed out here. First, the wizard adds automatically a new product_id column, which will be used as an internal primary key. For classes using internal identity, a column of type SQL INTEGER is automatically generated. 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.
How to Work with Entitieswith High-Low Identity Mechanism
Runtime, you don't need to perform any specific actions to work with entities with High-Low (internal) identity.
using (EntitiesModel dbContext = new EntitiesModel())
{
Product product = new Product();
product.Name = "product";
dbContext.Add(product);
dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
Dim _product As New Product()
_product.Name = "product"
dbContext.Add(_product)
dbContext.SaveChanges()
End Using
For example, the previous code will add two records in the database - one in the Product table and one in the voa_keygen table.
Another way to use the High-Low identity mechanism is to specify it explicitly by setting the Identity Mecahnism property to HighLow. The Identity Mechanism Member property will be automatically set to Id.
In contrast to the first example, in this case the Product entity has a primary key property. The Identity Mechanism is changed explicitly to HighLow. Note, that if you don't change the Identity Mechanism to HighLow, then Default Identity Mechanism will be used.
Composite Identity
In case your class has Composite Identity, in addition to specifying the Identity Mechanism you will have to manually specify which one of the identity members will be handled by the Identity Mechanism using the Identity Mechanism Member drop-down list.