Guid 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 Guid identity mechanism. Guid mechanism means that your identity field will require Guid values.

To complete this tutorial, you must have a basic understanding of Model First Mapping

Suppose, you are doing a Model First Mapping and you have an entity named Product. Set the Identity Mechanism for the Product entity to Guid. The Identity Mechanism Member will be automatically set to Id. The specific moment here is that the identity property of the Product entity is of type Guid.

When you create your database by using the Update Database from Model Wizard, the generated DDL script will be:

CREATE TABLE [product] (
   [nme] varchar(255) NULL,                -- _name
   [id] uniqueidentifier NOT NULL,         -- _id
   CONSTRAINT [pk_product] PRIMARY KEY ([id])
)
GO

The id column is of type uniqueidentifier.

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.

How to Work with Entities with Guid Identity Mechanism

The primary key value for entities with Guid identity mechanism could be automatically set by Telerik Data Access.

using (EntitiesModel dbContext = new EntitiesModel())
{
   Product product = new Product();
   dbContext.Add(product);
   dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
 Dim _product As New Product()
 dbContext.Add(_product)
 dbContext.SaveChanges()
End Using

However, you have the option to set explicitly the primary key value

using (EntitiesModel dbContext = new EntitiesModel())
{
   Product product = new Product();
   product.Id = Guid.NewGuid();
   dbContext.Add(product);
   dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
 Dim _product As New Product()
 _product.Id = Guid.NewGuid()
 dbContext.Add(_product)
 dbContext.SaveChanges()
End Using