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

How to: Define OpenAccessContext Class

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. This happens in the context class. The context is the channel used for retrieval of database objects and updating the database with the changes you make. With Telerik Data Access, this functionality is available to you through the OpenAccessContext class. In this demo, this is the DataContext class.

The DataContext class should inherit from OpenAccessContext. 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. It is delivered to you though the Telerik.DataAccess.Core NuGet package.

The context class should contain read-only properties that return an IQueryable of each type that you want to work with.

Let's declare in DataContext two IQueryable<T> properties exposing the Category and Product entities. You can paste the next code in the FluentModel.cs file.

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 How to: Create/Migrate Your Database, you will add code in the context class that will allow you to migrate your database to the latest model state.