Justin
Justin

Reputation: 18186

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?

Upvotes: 1

Views: 1394

Answers (2)

Punisher
Punisher

Reputation: 31

In my case - which was the same situation - I have added this to the InitializeAR method:

LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());

It ended up like this:

lock (typeof(SessionManager))
{
    if (!initialized)
    {
        LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());                
        System.Reflection.Assembly bin = typeof(SimpleModel).Assembly;
        IConfigurationSource s = (IConfigurationSource)ConfigurationManager.GetSection("activerecord");
        ActiveRecordStarter.Initialize(bin, s);

    }
    initialized = true;
}

Upvotes: 1

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99750

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

Upvotes: 0

Related Questions