How to: Manually Initialize 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.

This topic shows how to manually initialize an OpenAccessContext, using the different overloads of the OpenAccessContext class constructor. This topic assumes that you have already configured Telerik Data Access and defined a Telerik Data Access Domain Model.

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. For more information, see Database First Scenario.

Manually Initializing an OpenAccessContext

In order an OpenAccessContext to be initialized manually, the following parameters should be provided:

Connection String

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=(local);initial catalog=SofiaCarRental21;
                          integrated security=True" 
        providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>

However, there are scenarios which require separation of the Data Access Layer (Business Object Model) from the application.

Consider the previous snapshot. The SofiaCarRentalDAL project is a class library project containing your domain model. The DemoClient project is your executable project. Suppose, that in the DemoClient project you have added references to the SofiaCarRentalDAL, Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll assemblies. The DemoClient project is the main (executable) project for the application. When you run the application all settings (configurations) are taken from that project. Respectively the OpenAccessContext will try to retreive the connection string from the config file in the main project. If a such doesn't exist, the initialization of the OpenAccessContext will fail. You will be able to create a new instance of the OpenAccessContext. However, if you try to load data, you will receive a ConfigurationErrorsException.

In this case you have 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.

string connectionString = @"data source=(local);initial catalog=SofiaCarRental21;" + 
                          @"integrated security=True";
using (SofiaCarRentalDbContext dbContext = new SofiaCarRentalDbContext(connectionString))
{
   var items = dbContext.Cars.ToList();
}
Dim connectionString As String = "data source=(local);initial catalog=SofiaCarRental21;" & _
                                 "integrated security=True"
Using dbContext As New SofiaCarRentalDbContext(connectionString)
 Dim items = dbContext.Cars.ToList()
End Using

An alternative and easier approach is just to copy the App.Config file from the project containing the Data Access Layer to the executable project.

BackendConfiguration

The BackendConfiguration provides backend configuration settings affecting the runtime behavior of Telerik Data Access.

Telerik.OpenAccess.BackendConfiguration configuration = 
    new Telerik.OpenAccess.BackendConfiguration();
configuration.Backend = "mssql";
Dim configuration As New Telerik.OpenAccess.BackendConfiguration()
configuration.Backend = "mssql"

Metadata Source

Telerik Data Access maps a database to a persistent class model by either applying attributes or by using an external XML mapping file. A persistent class is just like any normal object class that you might define as part of your application, except that it is annotated with special information that associates it with a particular database table. These annotations are made either as custom attributes on your class declaration or as an external XML file. For more information, check out Xml Metadata Source and CLR Attributes Metadata Source.

The third parameter you should pass to the OpenAccessContext is an instance of the MetadataSource class. Depending on the Mapping Type (XML or Attributes), you should use either XmlMetadataSource or AttributesMetadataSource. For more information about how to use XmlMetadataSource and AttributesMetadataSource, see the Using XmlMetadataSource and Using AttributesMetadataSource topics.

If you use the Fluent Mapping API, then you need to pass a FluentMetadataSource. For more information, please refer to Overview.

Examples

The following examples demonstrate how to manually initialize an OpenAccessContext. It is assumed that you have already defined a Telerik Data Access Domain Model.

Example 1

In this example all three parameters are manually defined and passed to the OpenAccessContext.

string connectionString = @"data source=(local);initial catalog=SofiaCarRental21;" + 
                          @"integrated security=True";

Telerik.OpenAccess.BackendConfiguration configuration = 
    new Telerik.OpenAccess.BackendConfiguration();
configuration.Backend = "mssql";

Telerik.OpenAccess.Metadata.XmlMetadataSource metadataSource = 
    Telerik.OpenAccess.Metadata.XmlMetadataSource.FromAssemblyResource(
        Assembly.LoadFrom("SofiaCarRentalDAL.dll"), "SofiaCarRentalDomainModel.rlinq");

using (SofiaCarRentalDbContext dbContext = 
    new SofiaCarRentalDbContext(connectionString, configuration, metadataSource))
{
   var items = dbContext.Cars.ToList();
}
Dim connectionString As String = "data source=(local);initial catalog=SofiaCarRental21;" & _
                                 "integrated security=True"

Dim configuration As New Telerik.OpenAccess.BackendConfiguration()
configuration.Backend = "mssql"

Dim metadataSource As Telerik.OpenAccess.Metadata.XmlMetadataSource = _
    Telerik.OpenAccess.Metadata.XmlMetadataSource. _
        FromAssemblyResource(System.Reflection.Assembly.LoadFrom("SofiaCarRentalDAL.dll"),  _
            "SofiaCarRentalDomainModel.rlinq")

Using dbContext As New SofiaCarRentalDbContext(connectionString, configuration, metadataSource)
 Dim items = dbContext.Cars.ToList()
End Using

Example 2

In cases when the main (executable) project contains an App.Config file with a connection string section, you don't need to construct a connection string and metadata source.

BackendConfiguration configuration = new BackendConfiguration();
configuration.Backend = "mssql";
NorthwindDbContext dbContext = new NorthwindDbContext( configuration );
Dim configuration As New BackendConfiguration()
configuration.Backend = "mssql"
Dim dbContext As New NorthwindDbContext(configuration)