Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Set a Custom OpenAccessContext
Programmer's Guide > Developer's Guide > ASP.NET Development > Working with OpenAccessLinqDataSource > How to: Set a Custom OpenAccessContext

Glossary Item Box

By default, the OpenAccessLinqDataSource control creates an instance of the type that is specified in the ContextTypeName property. The OpenAccessLinqDataSource control calls the default constructor of the OpenAccessContext object to create an instance of the object. The context type object is created during select, update, insert, and delete operations. When using the OpenAccessLinqDataSource control, you can provide your own OpenAccessContext instance in the ContextCreating event. 

ASP.NET Copy Code
<telerik:OpenAccessLinqDataSource ID="OpenAccessLinqDataSource1"
                                 
runat="server"
                                 
ContextTypeName="OpenAccessLinqDataSourceDemo.EntitiesModel"
                                 
EntityTypeName=""
                                 
ResourceSetName="Categories"
                                 
OnContextCreating="OpenAccessLinqDataSource1_ContextCreating">
</
telerik:OpenAccessLinqDataSource>

The following example shows how to pass a custom instance of the OpenAccessContext to the OpenAccessLinqDataSource control. The code assigns the object to the ObjectInstance property. Note that the OpenAccessLinqDataSource disposes its context after each CRUD operation and using the same instance instead of creating a new one is currently not supported.

C# Copy Code
protected void OpenAccessLinqDataSource1_ContextCreating( object sender,
   Telerik.OpenAccess.Web.OpenAccessLinqDataSourceContextEventArgs e )
{
   
string connectionString = @"data source=.\sqlexpress;initial catalog=SofiaCarRental21;integrated security=True";
   e.ObjectInstance =
new EntitiesModel( connectionString );
}
VB.NET Copy Code
Protected Sub OpenAccessLinqDataSource1_ContextCreating(sender As Object,
                                                        e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceContextEventArgs) Handles OpenAccessLinqDataSource1.ContextCreating
    Dim connectionString As String = "data source=.\sqlexpress;initial catalog=SofiaCarRental21;integrated security=True"
    e.ObjectInstance = New EntitiesModel(connectionString)
End Sub