views:

86

answers:

1

Scenario: I have an application that pulls data from a SQL database as well as an Oracle database. I have NHibernate implemented for the SQL side and a co-worker already has a working implementation of the Oracle side (same object different project). I am currently defining a connection string in App.Config and calling this function in Program.cs

 var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Foo"].ToString();
 var configuration = InPlaceConfigurationSource.Build(DatabaseType.MsSqlServer2000, connectionString);
 ActiveRecordStarter.Initialize(System.Reflection.Assembly.GetExecutingAssembly(), configuration);

Note the project is in C# .Net 3.5

I have read about using DifferentDatabaseScope but when I try that the queries don't return any results nor can I see anything in the NHib Profiler. No errors pop up just 0 count.

Question: How do I implement multiple connections?

+1  A: 

For future users who come across this problem.
This article helps http://www.darkside.co.za/archive/2008/01/21/castle-activerecord-connecting-to-multiple-databases.aspx

To get this to work I had to add these code snippets to my App.config

<activerecord>

<config>
  <add key="connection.driver_class" 
      value="NHibernate.Driver.OracleClientDriver" />
  <add key="dialect"                 
      value="NHibernate.Dialect.Oracle10gDialect" />
  <add key="connection.provider"            
      value="NHibernate.Connection.DriverConnectionProvider" />
  <add key="connection.connection_string" 
      value="Data Source = 
              (DESCRIPTION = 
                 (ADDRESS = 
                    (PROTOCOL = TCP)
                    (HOST = SERVERNAME)
                    (PORT = 1521)
                 )
                 (ADDRESS = 
                    (PROTOCOL = TCP)
                    (HOST = SERVERNAME)
                    (PORT = 1521)
                 )
                 (LOAD_BALANCE = yes)
                 (CONNECT_DATA =
                    (SERVER = DEDICATED)
                    (SERVICE_NAME = NAME)
                 )
              );User Id = ID; Password = PASS;" />
</config>

And this

    <config type="Sens.SensClass`1, Sens">
      <add key="connection.driver_class" 
         value="NHibernate.Driver.SqlClientDriver" />
      <add key="dialect"                 
         value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="connection.provider"     
         value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="connection.connection_string" 
         value="Data Source=mntcon016\;Initial Catalog=TEST;Trusted_Connection=True;" />
    </config>
  </activerecord>


  <configSections>
    <section name="activerecord"
                type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property>
    </session-factory>
  </hibernate-configuration>

Then in Program.cs before Application.Run I have

 ActiveRecordStarter.Initialize(
               ActiveRecordSectionHandler.Instance, types.ToArray());

Where types is a list(function requires an array) of type[]. This list needs to contain every class that will be used with Nhibernate. In my case it contains both the SQL and Oracle classes. As well as this class that is inherited by all my SQL classes

public abstract class TestClass<T> : ActiveRecordBase<T>
{

}

To generate my SQL classes I had used a generator and it made them all Serializable which had to be taken off. Also note that you cannot have classes with the same name or you will get an error.

Gage