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

Obtaining an OpenAccessContext From an Object

When you use Telerik Data Access, all of your work with objects is done within an instance of the OpenAccessContext class. The OpenAccessContext represents the database connection and handles the details of the object lifecycle in your application. Therefore, in order to work with objects you first need to establish an OpenAccessContext instance, like in the example below:

using (FluentModel dbContext = new FluentModel())
Using dbContext As New FluentModel()
End Using

However, there might be situations when you have an access to a given persistent capable object, but not to the OpenAccessContext. It is possible to obtain the OpenAccessContext responsible for the object by using the OpenAccessContext.GetContext static method. This method takes a single parameter, an object, and returns the context responsible for the managing of the passed object. The OpenAccessContext.GetContext method can be used in situations where the context is not referred explicitly.

An example demonstrating the usage of this method is given below:

public static FluentModel ObtainActualContext(Category persistentCapableObject)
   return Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject) 
       as FluentModel;
Public Function ObtainActualContext(ByVal persistentCapableObject As Category) _
    As FluentModel
 Return TryCast(Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject),  _
End Function