How to: Extend the OpenAccessContext With Custom ADO Methods

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.

This topic demonstrates how to extend the generated OpenAccessContext with custom methods that use Telerik Data Access ADO API.

Using Partial Classes

When working with automatically generated source (like the domain classes and the OpenAccessContext generated by the Visual Designer), code can be added to the class without having to modify the original source file. Or in other words, you can extend your OpenAccessContext class without modifying the source file. Using partial classes is a perfect solution for separating auto-generated code from developer code.

Do not modify the original source files for the domain classes and OpenAccessContext. If you open the Visual Designer and re-save the RLINQ file, all changes will be overridden. Instead, add your custom methods and properties in a partial class.

Suppose you have the following domain model. EntitiesModel is the name of the OpenAccessContext class.

Basically, what you need to do is:

  1. Create a new class with the same name as the name of the context class. Use the following naming pattern: <ContextClassName>.partial.cs for C# or <ContextClassName>.partial.vb for VB.

  2. The new partial class should be in the same namespace as the original.

  3. Mark the class with the partial keyword.

    public partial class EntitiesModel
    {
    }
    
    Partial Public Class EntitiesModel
    End Class
    
  4. Add your custom action methods.

The following example demonstrates how to re-write the GetScalarAvarageValue method from the How to: Execute Queries that Return Scalar Values topic, so that it is now exposed by the context.

using System.Data;

namespace DemoApplication
{
   public partial class EntitiesModel
   {
       public decimal GetDailyAverage()
       {
           // 1. Initialize the sql query.
           const string SqlQuery = "Select Avg(Daily) from RentalRates";
           using ( IDbConnection connection = this.Connection )
           {
               // 2. Create a new instance of the OACommand class.
               using ( IDbCommand command = connection.CreateCommand() )
               {
                   command.CommandText = SqlQuery;
                   // 3. Execute the command and retrieve the scalar values.
                   decimal result = ( decimal )command.ExecuteScalar();
                   return result;
               }
           }
       }
   }
}
Partial Public Class EntitiesModel
    Public Function GetDailyAverage() As Decimal
        ' 1. Initialize the sql query.
        Const SqlQuery As String = "Select Avg(Daily) from RentalRates"
        Using connection As IDbConnection = Me.Connection
            ' 2. Create a new instance of the OACommand class.
            Using command As IDbCommand = connection.CreateCommand()
                command.CommandText = SqlQuery
                ' 3. Execute the command and retrieve the scalar values.
                Dim result As Decimal = CDec(command.ExecuteScalar())
                Return result
            End Using
        End Using
    End Function
End Class