Castle ActiveRecord change database in use

881 Views Asked by At

I'm creating a project where there will be a main database (where are some global things) and files (which are basically sqlite files).

Obviusly when the user clicks "open" and selects a new file, I would like that the application will load this new database.

Actually I managed to work with 2 configuration files http://www.darkside.co.za/archive/2008/01/21/castle-activerecord-connecting-to-multiple-databases.aspx

However, there are 2 problems: first, password is clear in xml file, while this could be ok for global things, it's not very good for files created by the user (I still don't know if this is a real problem, I have to ask if they want passwords for their files).

the otherone is that connection strings will be different for each file (yes I have to change the path!), so I can work in 2 ways:

  • Create a local copy of the db and when the user press "save", the database will be copied over the older one
  • Find a way to change the connection string or load a configuration "on runtime"

I'm looking forward DifferentDatabaseScope, but I don't understand: where i specify all NHibernate configuration if I use this? (I have to pass only a connection object!!!)

2

There are 2 best solutions below

0
On BEST ANSWER

There isn't a direct way to change database in use except at castle activerecord start, that's what I found.

0
On

The NHibernate configuration would be the same, you would use the same ActiveRecord class to do the query, just wrap the code you use to get the record in a using DifferentDatabaseScope

Using DifferentDatabaseScope

Another option to use is the DifferentDatabaseScope class. This class takes an IDbConnection object in the constructor which needs to be open, and makes use of this database connection to attempt to retrieve the data.

At present (in the RC3 build), there is a comment which notes that this isn't bullet proof. I'm sure they just left off the "yet" in the comments.

An example of doing this in code is:

IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=TestB;Integrated Security=SSPI");

connection.Open();

using (new DifferentDatabaseScope(connection))

{

    TestTableDatabaseA test3 = TestTableDatabaseA.Find(1);

    Console.WriteLine(test3.Title);

}