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:
<connectionString>data source=.\SQLEXPRESS;initial catalog=DatabaseOne;integrated security=True</connectionString>
To obtain an IObjectScope instance, use the corresponding ObjectScopeProvider class which is generated by the Enable project wizard:
If you have not generated those helper classes, you can use the Database.Get() static method as well:
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:
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.
Subscribe to be the first to get our expert-written articles and tutorials for developers!