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

Creating an OpenAccessContext

The OpenAccessContext class is the primary class for interacting with data as objects that are instances of persistent classes that are defined in a fluent model. The class that represents your context should derive from OpenAccessContext. This class should contain read-only properties that return an IQueryable of each type that you want to work with. You will 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 context. 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 (FluentModel dbContext = new FluentModel())
{
}
Using dbContext As New FluentModel()
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 of FluentMetadataSource (the class, in which you define the mapping configuration of your model, should inherit FluentMetadataSource). 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. It should reside in an App.config or Web.config file in the project that consumes Telerik Data Access. Usually, the connection string has the following format:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <connectionStrings>
   <add name="SofiaCarRentalConnection"
        connectionString="data source=.\sqlexpress;initial catalog=SofiaCarRental_v2.2;
                          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=SofiaCarRental_v2.2;" + 
                                    @"integrated security=True";
   return new FluentModel( ConnectrionString );
}
Private Shared Function CreateContext() As OpenAccessContext
 Const ConnectrionString As String = "data source=.\sqlexpress;" &
                                     "initial catalog=SofiaCarRental_v2.2;" &
                                     "integrated security=True"
 Return New FluentModel(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 = "SofiaCarRentalConnection";
   return new FluentModel( ConnectrionStringName );
}
Private Shared Function CreateContext() As OpenAccessContext
 Const ConnectrionStringName As String = "SofiaCarRentalConnection"
 Return New FluentModel(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 = new FluentModelMetadataSource();
   return new FluentModel( 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 = New FluentModelMetadataSource()
 Return New FluentModel(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. For this purpose, you need to use FluentMetadataSource.