Telerik blogs

Very often it is necessary to work simultaneously with more than one database. In such cases it is recommended that each database is mapped to a separate class model residing in its own assembly. The object scopes that handle the persistent objects should be obtained from an instance of the Database class which represents particular database on the server. To make this process easier Telerik OpenAccess ORM generates a helper class named ObjectScopeProvider. How to benefit from this class will be described later in this post.

To explain practically how to configure the Visual Studio projects, consider the case of having two databases (DatabaseOne and DatabaseTwo) mapped to two class library projects (DAL1 and DAL2) and a web site which operates with the persistent data. Enabling the persistent model projects to use Telerik OpenAccess ORM is done the standard way - just select a different database for each project. Then you can create the web site and enable it to use OpenAccess. Do not forget to add references to the DAL1 and DAL2 projects before running the Enable project wizard. OpenAccess will recognize them and add the corresponding <reference> nodes in the configuration file. Also make sure that the Build Action property of the two App.config files is set to Embedded resource. Otherwise OpenAccess will not be able to read their mapping configurations.

It is recommended to copy the two connection strings to the <connectionStrings> section of the Web.config. They will override the connection strings specified in the App.config files and you will be able to change them if necessary, without rebuilding the DAL assemblies.

At the end you should have a configuration similar to this:

DAL1 App.config:

<openaccess xmlns="http://www.telerik.com/OpenAccess">
   
<references />
    <
connections>
       
<connection id="DAL1Connection">
           
<connectionString>data source=.\SQLEXPRESS;initial catalog=DatabaseOne;integrated security=True</connectionString>
           
<backendconfigurationname>mssqlConfiguration</backendconfigurationname>
       
</connection>
   
</connections>
   
<backendconfigurations>
    …
   
</backendconfigurations>
   
<mappings current="mssqlMapping">
    …
   
</mappings>
</openaccess>

 
DAL2 App.config:
<openaccess xmlns="http://www.telerik.com/OpenAccess">
   
<references />
    <
connections>
       
<connection id="DAL2Connection">
           
<connectionString>data source=.\SQLEXPRESS;initial catalog=DatabaseTwo;integrated security=True</connectionString>
           
<backendconfigurationname>mssqlConfiguration</backendconfigurationname>
       
</connection>
   
</connections>
   
<backendconfigurations>
    …
   
</backendconfigurations>
   
<mappings current="mssqlMapping">
    …
   
</mappings>
</openaccess>
 
Web.config:
<connectionStrings>
   
<add name="DAL1Connection" connectionString="data source=.\SQLEXPRESS;initial catalog=DatabaseOne;integrated security=True" providerName="System.Data.SqlClient" />
    <
add name="DAL2Connection" connectionString="data source=.\SQLEXPRESS;initial catalog=DatabaseTwo;integrated security=True" providerName="System.Data.SqlClient" />
</
connectionStrings>

<openaccess xmlns="http://www.telerik.com/OpenAccess">
   
<references>
       
<reference assemblyname="DAL1" configrequired="True"/>
        <
reference assemblyname="DAL2" configrequired="True"/>
    </
references>
   
<connections/>
</openaccess>

To obtain an IObjectScope instance, use the corresponding ObjectScopeProvider class which is generated by the Enable project wizard:

IObjectScope scopeDAL1 = DAL1.ObjectScopeProvider1.GetNewObjectScope();
IObjectScope scopeDAL2 = DAL2.ObjectScopeProvider1.GetNewObjectScope();

If you have not generated those helper classes, you can use the Database.Get() static method as well:

IObjectScope scopeDAL1 = Database.Get("DAL1Connection").GetObjectScope();

In case you are using forward mapping, disable the UpdateDatabase property for all projects. This option enables OpenAccess to generate the database schema as a post-build step but since we do not want to rebuild the class libraries, we will handle the database generation at runtime. The following code should be executed for each persistent model before obtaining its first object scope:

Database db = DAL1.ObjectScopeProvider1.Database();
//Create the database if it does not exist
if (!db.GetSchemaHandler().DatabaseExists())
{
   
db.GetSchemaHandler().CreateDatabase();
}
//Update the schema if it differs from the expected
string script = db.GetSchemaHandler().CreateUpdateDDLScript(null);
if (!String.IsNullOrEmpty(script))
{
   
db.GetSchemaHandler().ExecuteDDLScript(script);
}

Hope this information helps to understand how to manage multiple database connections with Telerik OpenAccess ORM and keep the configuration as simple and flexible as possible.


Comments

Comments are disabled in preview mode.