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

Performing CRUD Operations

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.

Telerik Data Access enables you to work with domain class instances that represent data in the data source. You can create, update, and delete entities by using the OpenAccessContext. When the SaveChanges method of the OpenAccessContext is executed, Telerik Data Access generates and executes the statements that perform the equivalent insert, update, or delete statements against the database.

In this topic:

Inserting Entities

When you want to insert data, you must create a new instance of the domain class and add it to the context by using the Add method. Then you need to call the SaveChanges method. Before you can save the entity to the data source, you must first set all properties that do not support null values.

using (EntitiesModel dbContext = new EntitiesModel())
{
   Category category = new Category();
   dbContext.Add(category);
   dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
 Dim _category As New Category()
 dbContext.Add(_category)
 dbContext.SaveChanges()
End Using

Updating Entities

Performing an update operation is an extremely simple operation. You need a reference to an existing object in the database. Once you have it, just update its properties and invoke the well-known SaveChanges method.

using (EntitiesModel dbContext = new EntitiesModel())
{
   Category category = dbContext.Categories.FirstOrDefault();
   category.CategoryName = "Changed";
   dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
 Dim _category As Category = dbContext.Categories.FirstOrDefault()
 _category.CategoryName = "Changed"
 dbContext.SaveChanges()
End Using

Deleting Entities

In order to delete an object from the database the following steps should be performed:

  1. You need a reference to the target object.
  2. Pass the object to the OpenAccessContext's Delete method.
  3. Invoke the SaveChanges method.

For example:

using (EntitiesModel dbContext = new EntitiesModel())
{
   Category category = dbContext.Categories.FirstOrDefault();
   dbContext.Delete(category);
   dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
 Dim _category As Category = dbContext.Categories.FirstOrDefault()
 dbContext.Delete(_category)
 dbContext.SaveChanges()
End Using

Saving Changes

In order to write changes to the database you need to call the SaveChanges method of the OpenAccessContext instance.

dbContext.SaveChanges();
dbContext.SaveChanges()

The SaveChanges method has a second overload, which accepts a ConcurrencyConflictsProcessingMode enumeration. This enumeration defines the concurrency mode during commit. By default, Telerik Data Access implements an optimistic concurrency control. This means that locks are not held on data in the data store between the data is queried and the data is updated. Telerik Data Access checks for changes in the database before saving changes to the database. Any conflicts will cause an Telerik.OpenAccess.Exceptions.OptimisticVerificationException.

The StopOnFirst value will stop the processing after the first error. The other possible value is AggregateAll. It executes all insert, update and delete statements regardless of an error. If all failures during commit should be collected and not only the first one, then the AggregateAll mode should be used. This can be a time consuming operation.

Collecting all failures during commit can be a time consuming operation.

dbContext.SaveChanges(ConcurrencyConflictsProcessingMode.AggregateAll);
dbContext.SaveChanges(ConcurrencyConflictsProcessingMode.AggregateAll)