Search code examples
nhibernateactiverecordcastle-activerecord

Connecting to multiple databases in Active Record


I'm trying to connect to multiple databases in castle active record (which uses nhibernate.) My config file looks like this:

<configSections>
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <activerecord>
    <config type="Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.NavtrakOperationsDatabase`1, CommonSchemas">
      <add key="hibernate.connection.connection_string" value="myconnstring" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    </config>
    <config type="Navtrak.Business.Schemas.CommonSchemas.Models.Errors.ErrorsDatabase`1, CommonSchemas">
      <add key="hibernate.connection.connection_string" value="Data Source=myconnstring" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    </config>
  </activerecord>

And then I have a base abstract class for each database like this:

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

    }

And then each class inherits from this. I'm then initializing active record like this:

ActiveRecordStarter.Initialize(typeof(SimpleModel).Assembly, ActiveRecordSectionHandler.Instance);

Which gives me the error:

Could not find the dialect in the configuration

If I change the activation code to this:

ActiveRecordStarter.Initialize(
                    ActiveRecordSectionHandler.Instance,
                    typeof(NavtrakOperationsDatabase<>),
                    typeof(ErrorsDatabase<>)
                );

Then I get the following error:

You have accessed an ActiveRecord class that wasn't properly initialized. There are two possible explanations: that the call to ActiveRecordStarter.Initialize() didn't include Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application class, or that Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application class is not decorated with the [ActiveRecord] attribute.

I obviously don't want to include every single class in the Initialize function.

Any ideas?


Solution

  • Drop the "hibernate." prefix from all config keys. That prefix was used in NHibernate 1.x