How to: Obtain the OpenAccessContext From an Object
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.
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 (EntitiesModel dbContext = new EntitiesModel())
{
}
Using dbContext As New EntitiesModel()
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 EntitiesModel ObtainActualContext(Category persistentCapableObject)
{
return Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject)
as EntitiesModel;
}
Public Function ObtainActualContext(ByVal persistentCapableObject As Category) As EntitiesModel
Return TryCast(Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject), _
EntitiesModel)
End Function