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

How to: Obtain the 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 OpenAccessContextBase.GetContext static method. This method takes a single parameter, an object, and returns the context responsible for the managing of the passed object. The OpenAccessContextBase.GetContext method can be used in situations where the context is not referred explicitly.

It is recommended to use GetContext method from a central place in your class hierarchy in order to simplify the code.

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