Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Obtaining an OpenAccessContext From an Object
Programmer's Guide > Feature Reference > API > Context API > Obtaining an OpenAccessContext From an Object

Glossary Item Box

When you use Telerik OpenAccess ORM, 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:

C# Copy Code
using (EntitiesModel dbContext = new EntitiesModel())
{
}
VB.NET Copy Code
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:

C# Copy Code
public static EntitiesModel ObtainActualContext(Category persistentCapableObject)
{
   
return Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject) as EntitiesModel;
}
VB.NET Copy Code
Public Function ObtainActualContext(ByVal persistentCapableObject As Category) As EntitiesModel
 Return TryCast(Telerik.OpenAccess.OpenAccessContextBase.GetContext(persistentCapableObject), EntitiesModel)
End Function