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

How to: Manually Initialize an OpenAccessContext

This topic shows how to manually initialize an OpenAccessContext, using the different overloads of the OpenAccessContext class constructor. This topic assumes that you have a SofiaCarRental model.

Manually Initializing an OpenAccessContext

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

Connection String

The SofiaCarRental.Model project is a class library project containing your fluent model. When you add a new console application project (called DemoClient for example), integrate it with the Core NuGet package, and add a reference to the SofiaCarRental.Model project you will be able to code a few CRUD operations against the database. When you run the application all settings (configurations) are taken from that project. Respectively the OpenAccessContext will try to retrieve the connection string from the config file in the DemoClient project. If 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=SofiaCarRental_v2.2;" + 
                          @"integrated security=True";
using (SofiaCarRentalDbContext dbContext = new SofiaCarRentalDbContext(connectionString))
{
   var items = dbContext.Cars.ToList();
}
Dim connectionString As String = "data source=(local);initial catalog=SofiaCarRental_v2.2;" & _
                                 "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

The third parameter you should pass to the OpenAccessContext is an instance of the MetadataSource class. The Fluent Mapping API of Data Access consumes a special metadatasource type - 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 model.

Example 1

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

string connectionString = @"data source=(local);initial catalog=SofiaCarRental_v2.2;" + 
                          @"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=SofiaCarRental_v2.2;" & _
                                 "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)