连接到Active Record中的多个数据库

时间:2011-02-01 21:38:43

标签: nhibernate activerecord castle-activerecord

我正在尝试连接到城堡活动记录中的多个数据库(使用nhibernate。)我的配置文件如下所示:

<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>

然后我为每个数据库都有一个基本抽象类:

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

    }

然后每个类继承自此。我正在初始化这样的活动记录:

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

这给了我错误:

无法在配置中找到方言

如果我将激活码更改为:

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

然后我收到以下错误:

您访问过未正确初始化的ActiveRecord类。有两种可能的解释:对ActiveRecordStarter.Initialize()的调用不包括Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application类,或者Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.Application类未使用[ActiveRecord]属性进行修饰。

我显然不想在Initialize函数中包含每一个类。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在我的情况下 - 这是相同的情况 - 我已经将它添加到InitializeAR方法:

LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());

它最终是这样的:

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;
}

答案 1 :(得分:0)

放弃“休眠”。所有配置键的前缀。该前缀用于NHibernate 1.x