无法使ActiveRecordSectionHandler正常工作

时间:2014-03-24 16:18:32

标签: c# nhibernate activerecord app-config castle-activerecord

我在测试项目中设置了以下配置:

<configuration>
  <configSections>
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
    ...
  </configSections>
  ...
  <activerecord>
    <config>
      <add key="connection.provider" value="(MyNamespace).Tests.Helpers.TestingConnectionProvider, (MyNamespace).Tests" />
      <add key="dialect" value="NHibernate.Dialect.NHibernate.Dialect.SQLiteDialect" />
      <add key="connection.driver_class" value="NHibernate.Driver.SQLiteDriver" />
      <add key="connection.connection_string" value="Data Source=:memory:;Version=3;New=True;" />
      <add key="show_sql" value="true" />
      <add key="query.substitutions" value="true=1;false=0" />
      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
    </config>
  </activerecord>
  ...
</configuration>

我还在键中添加了nhibernate前缀,正如一些文档所建议的那样。我的连接提供商看起来像:

public class TestingConnectionProvider : DriverConnectionProvider
{
    public static IDbConnection Connection { get; private set; }

    public override IDbConnection GetConnection()
    {
        return Connection ?? (Connection = base.GetConnection());
    }
}

当我尝试运行我的测试时:

[TestClass]
public class PersistenceTests
{
    [TestMethod]
    public void CanBuildSchemaInMemory()
    {
        if (File.Exists("SqlCreate.sql")) File.Delete("SqlCreate.sql");
        ActiveRecordStarter.Initialize(typeof(IPermissionsManager).Assembly, new ActiveRecordSectionHandler());
        ActiveRecordStarter.GenerateCreationScripts("CreateNew.sql");
        Assert.IsTrue(File.Exists("SqlCreate.sql"));
    }
}

...我失败了,表明我没有正确设置。但是,如果我将ActiveRecordSectionHandler()更改为:

InPlaceConfigurationSource.Build(DatabaseType.MsSqlServer2008, "Server=localhost, Initial Catalog=Permissions, Integrated Security=SSPI;")

......这很有效,很明显问题出在我的配置上。我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,这是基于我所做的一些愚蠢的事情,以及对其他一些事情的真正缺乏了解。

斩首事物#1:我生成的文件不是我要找的名字。干得好,杰里米。

Boneheaded thing#2:我的配置文件中有额外的命名空间元素;它不是NHibernate.Dialect.NHibernate.Dialect.SQLiteDialect,只是NHibernate.Dialect.SQLiteDialect

真正的误解:我认为ActiveRecordSectionHandler本来是要实例化的;事实证明,我需要Instance的静态属性才能工作。所以看起来应该是:

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

瞧!考试通过了。