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

How to: Manage ConnectionStrings

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 provides an example on how to manage connection strings in n-tier applications.

N-tier application architecture provides a model for developers to create a flexible and reusable application. By breaking up an application into tiers, developers only have to modify or add a specific layer, rather than rewriting the entire application again. There should be a presentation tier, a business or data access tier, and a data tier.

By default, if you use the Telerik Data Access New Domain 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/Web.config file (depending on your project type) in the project, which contains a section with the connection string to the target database.

The connection string in the configuration file is also used by the Update Database from Model and Update from Database wizards. If there isn't a database connection and you run any of these wizards, the first screen will be the Setup Database Connection dialog. There, you need to specify the data source, connection options, and database to be used in your application.

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

The WebApplication project contains references to OpenAccessModel, Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll. Suppose, you have the following code in your main (start up) project.

using (EntitiesModel dbContext = new EntitiesModel())
{
   Customer customer = dbContext.Customers.FirstOrDefault();
}
Using dbContext As New EntitiesModel()
 Dim _customer As Customer = dbContext.Customers.FirstOrDefault()
End Using

When you try to execute the previous code in your executable project, you will receive an exception saying something like this: System.Configuration.ConfigurationErrorsException: A connection string was referenced by name 'XXXConnection' but could not be resolved through the web.config or the executables app.config file.

The reason is that all settings (including the connection string) are taken from the executable project (e.g. the WebApplication project). By default, all database connection details are part of the App.config file that belongs to the class library (e.g. the OpenAccessModel project) that contains the domain model. Having in mind that the class library is deployed as a compiled assembly and it contains the App.config file as an embedded resource, the OpenAccessContext is unable to retrieve the correct connection string.

The solution is:

  1. Open the App.config file from the project that contains the domain model.
  2. Locate the <connectionStrings> section.

    <?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>
    
  3. Copy the connection string.

  4. Open the Web.config file of your project. In case your project doesn't contain a configuration file, you will need to add a new one.
  5. Paste the connection string in the corresponding <connectionStrings> section of the config file in the main (executable) project.

Constructing ConnectionString Manually

An alternative approach is to initialize the OpenAccessContext manually, by using the different overloads of the OpenAccessContext class constructor. For more information, see How to: Manually Initialize an OpenAccessContext.

See Also