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

How to: Set a Custom OpenAccessContext

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.

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.

<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.

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 );
}
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