Data Synchronization
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.
There are cases where you might want to update columns in the database but to leave the auto-calculated fields unchanged. In Telerik Data Access auto-calculated fields are version information and auto-set DateTime columns. Currently, Telerik Data Access allows a DateTime field to be automatically calculated when a CUD operation is executed. Such configuration is considered advanced and it is only possible with Fluent Mapping API. In cases where you want only to synchronize your data (e.g. when the same records are presented in two databases), you might want to set an explicit value to that DateTime field.
For such scenarios, Telerik Data Access includes a special mode which controls the behavior of the OpenAccessContext class instances during Insert and Update operations. This mode can be enabled via the EnableDataSynchronization property. This property is accessible through the OpenAccessContextOptions object that is exposed by the OpenAccessContext.
using (EntitiesModel dbContext = new EntitiesModel())
{
dbContext.ContextOptions.EnableDataSynchronization = true;
}
Using dbContext As New EntitiesModel()
dbContext.ContextOptions.EnableDataSynchronization = True
End Using
When the context is in a synchronization mode, any insert or update operations will not affect the auto-set DateTime columns configured with Fluent Mapping API. Another field that is also affected by this behavior is the version field when you use version as a concurrency control mechanism. The version number is incremented on every update. When EnableDataSynchronization is set to True, the version field is not changed on update operations and it is not set at all on insert operations. That’s why when you insert records in synchronization mode, you have to manually set a value to the version fields; otherwise, an exception will be thrown during SaveChanges.The EnableDataSynchronization is not a one-time setting. It can be switched on and off during the lifetime of the context.
Suppose, you have the following table on the server.
The DateTime column will be used for concurrency checks. You have a domain model including the Suppliers table. The Concurrency Mode for the Supplier entity is set to Timestamp and the Concurrency Member is set to the ModifiedDate column.
Each time a record is updated/inserted, the datetime column will be updated automatically by Telerik Data Access. However, you could prevent this by setting the EnableDataSynchronization property to True. Here is an example:
using (EntitiesModel dbContext = new EntitiesModel())
{
Supplier firstSupplier = dbContext.Suppliers.FirstOrDefault();
firstSupplier.Name = "John";
// The ModifiedDate column will be automatically updated by Telerik Data Access.
dbContext.SaveChanges();
dbContext.ContextOptions.EnableDataSynchronization = true;
firstSupplier.Name = "Ivan";
// This time the ModifiedDate column won't be updated.
dbContext.SaveChanges();
// The EnableDataSynchronization setting is not a one-time only setting.
dbContext.ContextOptions.EnableDataSynchronization = false;
dbContext.Name = "Josh";
dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
Dim firstSupplier As Supplier = dbContext.Suppliers.FirstOrDefault()
firstSupplier.Name = "John"
' The ModifiedDate column will be automatically updated by Telerik Data Access.
dbContext.SaveChanges()
dbContext.ContextOptions.EnableDataSynchronization = True
firstSupplier.Name = "Ivan"
' This time the ModifiedDate column won't be updated.
dbContext.SaveChanges()
' The EnableDataSynchronization setting is not a one-time only setting.
dbContext.ContextOptions.EnableDataSynchronization = False
dbContext.Name = "Josh"
dbContext.SaveChanges()
End Using
Limitations
- It is not possible to insert explicit values in a SQL Server timestamp column.
- It is not possible to update a record without updating the value in a Timestamp column. This means that, in the cases where you have configured a backend-specific concurrency control (e.g. Timestamp), Telerik Data Access won’t be able to keep the original value after data synchronization.