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

Tracking Context Changes

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 allows you to track when objects are:

The OpenAccessContext.Events property returns an IContextEvents instance representing the associated changed tracking instance. Via the IContextEvents object, you could attach to the following events:

  • Added - occurs after an object was marked as to be persisted by the object context.
  • Adding - occurs before a persistent object is added to the object context.
  • Changed - occurs after a field of a persistent object has been changed by the application.
  • Changing - occurs before a field of a persistent object is changed by the application.
  • Refreshed - occurs after a persistent object is refreshed from the database.
  • Refreshing - occurs before a persistent object is refreshed from the database.
  • Removed - occurs after a persistent object was marked as to be removed from the object context.
  • Removing - occurs before a persistent object is removed from the object context.
  • ObjectConstructed - occurs after an object has been constructed by Telerik Data Access runtime.

All "XXXing" events are could be cancelled by setting the Cancel property of the event arguments to True.

Detecting When Objects Are Added to the Context

The first two events (Added and Adding) allow you to track when objects are added to the context.

public void TestMethod()
{
   using (EntitiesModel dbContext = new EntitiesModel())
   {
       dbContext.Events.Adding += new Telerik.OpenAccess.AddEventHandler(Events_Adding);
       dbContext.Events.Added += new Telerik.OpenAccess.AddEventHandler(Events_Added);
   }
}

void Events_Added(object sender, Telerik.OpenAccess.AddEventArgs e)
{
   // Getting the added object.
   object addedObject = e.PersistentObject;
}

private void Events_Adding(object sender, Telerik.OpenAccess.AddEventArgs e)
{
   // Getting the added object.
   object addedObject = e.PersistentObject;
   // The Adding event could be cancelled.
   e.Cancel = true;
}
Public Sub TestMethod()
 Using dbContext As New EntitiesModel()
  AddHandler dbContext.Events.Adding, AddressOf Events_Adding
  AddHandler dbContext.Events.Added, AddressOf Events_Added
 End Using
End Sub

Private Sub Events_Added(ByVal sender As Object, ByVal e As Telerik.OpenAccess.AddEventArgs)
 ' Getting the added object.
 Dim addedObject As Object = e.PersistentObject
End Sub

Private Sub Events_Adding(ByVal sender As Object, ByVal e As Telerik.OpenAccess.AddEventArgs)
 ' Getting the added object.
 Dim addedObject As Object = e.PersistentObject
 ' The Adding event could be cancelled.
 e.Cancel = True
End Sub

Detecting When Objects Are Updated

The Changing and Changed events allow you to track when objects hold by the context are updated.

public void TestMethod()
{
   using (EntitiesModel dbContext = new EntitiesModel())
   {
       dbContext.Events.Changing += new Telerik.OpenAccess.ChangeEventHandler(Events_Changing);
       dbContext.Events.Changed += new Telerik.OpenAccess.ChangeEventHandler(Events_Changed);
   }
}

private void Events_Changed(object sender, Telerik.OpenAccess.ChangeEventArgs e)
{
   // Getting the modified object.
   object modifiedObject = e.PersistentObject;

   // Getting the modified property name.
   string modifiedPropertyName = e.PropertyName;

   // Getting the new value.
   object newValue = e.NewValue;

   // Getting the old value.
   object oldValue = e.OldValue;
}

private void Events_Changing(object sender, Telerik.OpenAccess.ChangeEventArgs e)
{
   // Getting the modified object.
   object modifiedObject = e.PersistentObject;

   // The Changing event could be cancelled.
   e.Cancel = true;

   // Getting the modified property name.
   string modifiedPropertyName = e.PropertyName;

   // Getting the new value.
   object newValue = e.NewValue;

   // Getting the old value.
   object oldValue = e.OldValue;
}
Public Sub TestMethod()
 Using dbContext As New EntitiesModel()
  AddHandler dbContext.Events.Changing, AddressOf Events_Changing
  AddHandler dbContext.Events.Changed, AddressOf Events_Changed
 End Using
End Sub

Private Sub Events_Changed(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.ChangeEventArgs)
 ' Getting the modified object.
 Dim modifiedObject As Object = e.PersistentObject

 ' Getting the modified property name.
 Dim modifiedPropertyName As String = e.PropertyName

 ' Getting the new value.
 Dim newValue As Object = e.NewValue

 ' Getting the old value.
 Dim oldValue As Object = e.OldValue
End Sub

Private Sub Events_Changing(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.ChangeEventArgs)
 ' Getting the modified object.
 Dim modifiedObject As Object = e.PersistentObject

 ' The Changing event could be cancelled.
 e.Cancel = True

 ' Getting the modified property name.
 Dim modifiedPropertyName As String = e.PropertyName

 ' Getting the new value.
 Dim newValue As Object = e.NewValue

 ' Getting the old value.
 Dim oldValue As Object = e.OldValue
End Sub

Detecting When Objects are Deleted From the Context

The Removing and Removed events allow you to track when objects hold by the context are deleted.

public void TestMethod()
{
   using (EntitiesModel dbContext = new EntitiesModel())
   {
       dbContext.Events.Removing += new Telerik.OpenAccess.RemoveEventHandler(Events_Removing);
       dbContext.Events.Removed += new Telerik.OpenAccess.RemoveEventHandler(Events_Removed);
   }
}

private void Events_Removed(object sender, Telerik.OpenAccess.RemoveEventArgs e)
{
   // Get the object that will be deleted.
   object objectToDelete = e.PersistentObject;
}

private void Events_Removing(object sender, Telerik.OpenAccess.RemoveEventArgs e)
{
   // The Removing event could be cancelled.
   e.Cancel = true;
   // Get the object that will be deleted.
   object objectToDelete = e.PersistentObject;
}
Public Sub TestMethod()
 Using dbContext As New EntitiesModel()
  AddHandler dbContext.Events.Removing, AddressOf Events_Removing
  AddHandler dbContext.Events.Removed, AddressOf Events_Removed
 End Using
End Sub

Private Sub Events_Removed(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.RemoveEventArgs)
 ' Get the object that will be deleted.
 Dim objectToDelete As Object = e.PersistentObject
End Sub

Private Sub Events_Removing(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.RemoveEventArgs)
 ' The Removing event could be cancelled.
 e.Cancel = True
 ' Get the object that will be deleted.
 Dim objectToDelete As Object = e.PersistentObject
End Sub

Detecting When Objects Are Constructed By the Telerik Data Access Runtime

The ObjectConstructed event occurs when a new entity object is created from data in the data source as part of a query or load operation. If the object already exists in the OpenAccessContext, Telerik Data Access will not recreate the object and this event will not be raised. The event occurs after all properties have been set on an object.

public void TestMethod()
{
   using ( EntitiesModel dbContext = new EntitiesModel() )
   {
       dbContext.Events.ObjectConstructed += ObjectConstructed;

       Category category = dbContext.Categories.FirstOrDefault();
   }
}

private void ObjectConstructed( object sender, Telerik.OpenAccess.ObjectConstructedEventArgs e )
{
   Category constructedCategory = e.PersistentObject as Category;
}
Public Sub TestMethod()
    Using dbContext As New EntitiesModel()
        AddHandler dbContext.Events.ObjectConstructed, AddressOf ObjectConstructed

        Dim _category As Category = dbContext.Categories.FirstOrDefault()
    End Using
End Sub

Private Sub ObjectConstructed(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.ObjectConstructedEventArgs)
    Dim constructedCategory As Category = TryCast(e.PersistentObject, Category)
End Sub