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

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

The OpenAccessContext class is the primary class for interacting with data as objects that are instances of domain classes that are defined in a domain (or fluent) model. When the object layer that represents a domain model is generated by the Telerik Data Access Visual Designer, the class that represents the domain context is derived from the OpenAccessContext. This class contains read-only properties that return an IQueryable of each type that you want to work with. You can use these properties to read the objects you want from the database. Before starting CRUD operations against the model, you need to create a new instance of the OpenAccessContext. It is recommended to create a new instance of the context in the using statement. Thus, you will make sure that the context is properly disposed.

using (EntitiesModel dbContext = new EntitiesModel())
{
}
Using dbContext As New EntitiesModel()
End Using

An instance of the OpenAccessContext class encapsulates the following:

  • ConnectionString - a string representing either the connection string to the backend database or the name of the connection string defined in the configuration file. This is a required parameter. Read more
  • BackendConfiguration - backend configuration settings affecting the runtime behavior of Telerik Data Access. For some backends the backend type cannot be derived from the connection string and it must be specified explicitly. Read more
  • MetadataSource/MetadataContainer - the MetadataSource class is a base class for all mapping sources (xml, attributes or fluent) used by Telerik Data Access. The MetadataSource class exposes the meta model used by Telerik Data Access runtime. This is the MetadataContainer class. It is a central runtime API that you can use to interact with the Telerik Data Access metadata in the context of an application. The MetadataContainer class represents a container that will be mapped to a database object in the storage. The MetadataContainer class can describe all persistant storage artifacts like tables, views, stored procedures, indexes, constraints, etc. It also can describe all conceptual level artifacts like classes, properties, fields, relationships, etc. Read more

ConnectionString

The connectionString is a string that contains information that is required to connect to a data source. By default, if you use the Telerik Data Access Create Model Wizard in a Visual Studio project, it automatically generates a Domain Model and configures the project to use Telerik Data Access. That includes adding a new App.config or Web.config file in the project, which contains a section with the connection string to the target database.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <connectionStrings>
   <add name="SofiaCarRental21Connection"
        connectionString="data source=.\sqlexpress;initial catalog=SofiaCarRental21;
                          integrated security=True"
        providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>

Passing a Manually Constructed Connection String to the Context

The following example demonstrates how to manually construct and pass a connection string to the OpenAccessContext. In the connection string, the data source (server), database (initial catalog) and security parameters should be specified.

Note that the Telerik Data Access metadata is initialized per unique connectrion string. That means that different connection strings would lead to a new set of metadata to be loaded. This could be a performance issue if it is done frequently.

private static OpenAccessContext CreateContext()
{
   const string ConnectrionString = @"data source=.\sqlexpress;" + 
                                    @"initial catalog=SofiaCarRental21;" + 
                                    @"integrated security=True";
   return new EntitiesModel( ConnectrionString );
}
Private Shared Function CreateContext() As OpenAccessContext
 Const ConnectrionString As String = "data source=.\sqlexpress;" &
                                     "initial catalog=SofiaCarRental21;" &
                                     "integrated security=True"
 Return New EntitiesModel(ConnectrionString)
End Function

Passing a Connection String Name to the Context

A named connection string can be supplied instead of the connectionString parameter when instantiating the OpenAccessContext class. This is the name of the connection string in your app(web).config file.

private static OpenAccessContext CreateContext()
{
   const string ConnectrionStringName = "SofiaCarRental21Connection";
   return new EntitiesModel( ConnectrionStringName );
}
Private Shared Function CreateContext() As OpenAccessContext
 Const ConnectrionStringName As String = "SofiaCarRental21Connection"
 Return New EntitiesModel(ConnectrionStringName)
End Function

Note that, the app(web).config file should be located in the executable project.


BackendConfiguration

The BackendConfiguration provides backend configuration settings affecting the runtime behavior of Telerik Data Access. The following example demonstrates how to pass a new BackendConfiguration object to the OpenAccessContext.

private static OpenAccessContext CreateContext()
{
   const string ConnectrionStringName = "SofiaCarRental21Connection";
   BackendConfiguration backend = new BackendConfiguration
   {
       Backend = "MsSql",
       ProviderName = "System.Data.SqlClient"
   };
   MetadataSource metadataSource = XmlMetadataSource.FromAssemblyResource( "EntityDiagrams.rlinq" );
   return new EntitiesModel( ConnectrionStringName, backend, metadataSource );
}
Private Shared Function CreateContext() As OpenAccessContext
 Const ConnectrionStringName As String = "SofiaCarRental21Connection"
 Dim backend As BackendConfiguration = New BackendConfiguration With _
 {
     .Backend = "MsSql", 
     .ProviderName = "System.Data.SqlClient"
 }
 Dim _metadataSource As MetadataSource = XmlMetadataSource.FromAssemblyResource("EntityDiagrams.rlinq")
 Return New EntitiesModel(ConnectrionStringName, backend, _metadataSource)
End Function

For more information, see the Backend Configuration section.

MetadataSource/MetadataContainer

The third parameter you should pass to the OpenAccessContext is an instance of the MetadataSource class. Depending on the Mapping Type (XML, Attributes or Fluent), you can use XmlMetadataSource, AttributesMetadataSource or FluentMetadataSource.