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

How to: Define OpenAccessContext Class

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.

When you use Fluent Mapping API, your classes have no knowledge at all about the Telerik Data Access. This is a good thing, as it is the desired effect. However, you need to let Telerik Data Access be aware of the classes. Recall that the Telerik Data Access Add Model Wizard does not only create the entity classes, but also creates a class that inherits from OpenAccessContext. When you use the Telerik Data Access Fluent Library template, a custom context class is automatically created for you. The OpenAccessContext will do its job of querying and managing the custom classes. In this demo, this is the EntitiesModel class.

The EntitiesModel class inherits from OpenAccessContext just as the context classes you have seen so far. The custom context class should pass the following parameters to the base constructor:

  • A string representing the database connection. It is also possible to pass the name of the connection string in the app(web).config file.
  • The fluent metadata source is the class that holds the entire configuration for your classes.
  • The last parameter is a BackendConfiguration object. In some cases that parameter may be omitted (more explanations will be presented later).

The OpenAccessContext class is located in the Telerik.OpenAccess.35.Extensions.dll assembly. The other assembly you have to refer in your project is Telerik.OpenAccess.dll.

As with the other OpenAccessContext classes, this class contains read-only properties that return an IQueryable of each type that you want to work with.

Declares two IQueryable<T> properties exposing the Category and Product entities.

using System.Linq;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Metadata;
namespace Data
{
   public partial class DataContext : OpenAccessContext
   {
       static MetadataContainer metadataContainer = new DataMetadataSource().GetModel();
       static BackendConfiguration backendConfiguration = new BackendConfiguration()
       {
           Backend = "mssql"
       };
       private const string DbConnection = "connectionID";
       public DataContext()
           : base( DbConnection, backendConfiguration, metadataContainer )
       {
       }

       public IQueryable<Product> Products
       {
           get
           {
               return this.GetAll<Product>();
           }
       }

       public IQueryable<Category> Categories
       {
           get
           {
               return this.GetAll<Category>();
           }
       }
       // Rest of the code  
   }
}
Imports Telerik.OpenAccess
Imports Telerik.OpenAccess.Metadata
Partial Public Class DataContext
    Inherits OpenAccessContext
    Shared metadataContainer As MetadataContainer = (New DataMetadataSource()).GetModel()
    Shared backendConfiguration As BackendConfiguration = New BackendConfiguration() With
        {
            .Backend = "mssql"
        }
    Public Const DbConnection As String = "connectionId"
    Public Sub New()
        MyBase.New(DbConnection, backendConfiguration, metadataContainer)
    End Sub

    Public ReadOnly Property Products() As IQueryable(Of Product)
        Get
            Return Me.GetAll(Of Product)()
        End Get
    End Property

    Public ReadOnly Property Categories() As IQueryable(Of Category)
        Get
            Return Me.GetAll(Of Category)()
        End Get
    End Property
    'Rest of the code
End Class

In the next topic, you will add code in the context class that will allow you to migrate your database to the latest model state.