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

How to: Set a Custom OpenAccessContext

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="OpenAccessLinqDataSourceCategory" runat="server"
    ContextTypeName="SofiaCarRental.Model.FluentModel"
    ResourceSetName="Categories" EntityTypeName=""
    OnContextCreating="OpenAccessLinqDataSourceCategory_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 OpenAccessLinqDataSourceCategory_ContextCreating( object sender,
   Telerik.OpenAccess.Web.OpenAccessLinqDataSourceContextEventArgs e )
{
   string connectionString = @"data source=.\sqlexpress;initial catalog=SofiaCarRental_v2.2;" + 
      @"integrated security=True";
   e.ObjectInstance = new FluentModel( connectionString );
}
Protected Sub OpenAccessLinqDataSourceCategory_ContextCreating(sender As Object,
    e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceContextEventArgs) _
    Handles OpenAccessLinqDataSource1.ContextCreating
    Dim connectionString As String = "data source=.\sqlexpress;" + _
        "initial catalog=SofiaCarRental_v2.2;integrated security=True"
    e.ObjectInstance = New FluentModel(connectionString)
End Sub