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

Default 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 Default identity mechanism. Default mechanism means that Telerik Data Access will not count on any sources of information about the primary key column value. The primary key value has to be set explicitly by you in the code. As its name suggests, this is the default value for the Identity Mechanism property.

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. The Identity Mechanism for the Product entity is set to Default.

When you create your database by using the Update Database from Model Wizard, the Id column will be the primary key for the Product table.

How to Work with Entities with Default Identity Mechanism

The primary key value for entities with Default identity mechanism has to be set explicitly by you in the code.

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

Note that the default value for an Int32 property is 0. So, the following code will execute successfully (only the first time):

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

This will insert a new record with Id equals to 0. If you try to run the last code sample twice or more, you will receive a DuplicateKeyException.