CRUD Operations - API Differences
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.
The purpose of this topic is to illustrate you the most important API differences between Telerik Data Access and NHibernate regarding the CRUD operations. After converting your project from NHibernate to Telerik Data Access, it may be no longer buildable. In most of the cases the reason for this will be the various API differences.
Using the OpenAccessContext
The OpenAccessContext is the main runtime class you will use when working with Telerik Data Access. This is the central API class providing methods for CRUD operations. It can either be generated by the Add New Domain Model Wizard, or defined by you if you are using the code-only approach. Basically, the OpenAccessContext is equivalent to the ISession object in NHibernate.
using ( EntitiesModel dbContext = new EntitiesModel() )
{
}
Using dbContext As New EntitiesModel()
End Using
Handling Transactions
A transaction is an indivisible unit of work and it is used to ensure data integrity. Transactions control the concurrent access of data by multiple programs. In the event of a system failure, transactions ensure that after recovery the data is in a consistent state. All operations on a persistent class need to be carried out within a transaction. Transactions are started automatically and all changes are persisted in-memory. A transaction can end in two ways: with a commit or a rollback. OpenAccessContext.SaveChanges() attempts to save all the changes to the database, however, if any statement within the transaction fails, the transaction is rolled back, undoing all the effects of the statements in the transaction.
All the changes you make using a single context are considered a transaction, which can be committed through the SaveChanges method. You can also perform a rollback through the ClearChanges method. A new transaction is started automatically afterwards.
Inserting Objects
To add a new object in Telerik Data Access, you need to use the Add method of the context.
dbContext.Add(customer);
dbContext.Add(customer)
For example, you will need to replace the corresponding NHibernate API call - session.Save(customer).
Deleting Objects
In Telerik Data Access, you delete objects by invoking the Delete method of the OpenAccessContext instance. You need to replace the corresponding NHibernate method - session.Delete(customer).
dbContext.Delete(customer);
dbContext.Delete(customer)
Saving Changes
In Telerik Data Access you save changes to the underlying database by invoking the SaveChanges method of the OpenAccessContext instance.
dbContext.SaveChanges();
dbContext.SaveChanges()
References
For further reference, see the CRUD Operations section.