Obtaining an 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 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 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