1

我在设置 NHibernate 配置时遇到问题。如果我创建一个单独的配置文件,我可以让它工作,然后当我调用 Configuration.configure 我指向配置文件的绝对路径(“C:\我的文档......等等......)。这显然会当我尝试从开发转向生产时会出现问题。所以我想我会将配置移动到 web.config 文件,但它失败了。所以,在 web.config 文件中我有:

<configSections>
<section name="hibernate-configuration"
  type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
  <property name="connection.connection_string">Data Source=MyServerName;Initial Catalog=MyDatabase;Integrated Security=True</property>
  <property name="connection.isolation">ReadCommitted</property>
  <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
  <property name="current_session_context_class">web</property>
</session-factory>
</hibernate-configuration>

然后我像这样测试:

<TestFixture()> _
Public Class GenerateSchema_Fixture

    <Test()> _
    Public Sub Can_generate_schema()

        Dim cfg = New NHibernate.Cfg.Configuration()
        cfg.Configure() 
        cfg.AddAssembly(GetType(Question).Assembly)
        Dim exp As NHibernate.Tool.hbm2ddl.SchemaExport = New NHibernate.Tool.hbm2ddl.SchemaExport(cfg)
        exp.Execute(False, True, False)

    End Sub


End Class

当我运行这个测试时,我得到:

------ Test started: Assembly: ECEHire.Test.dll ------

Test 'ECEHire.Test.Tests.GenerateSchema_Fixture.Can_generate_schema' failed: NHibernate.HibernateException : Format of the initialization string does not conform to specification starting at index 49.

----> System.ArgumentException : 初始化字符串的格式不符合从索引 49 开始的规范。在 NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action 1 scriptAction, Boolean export, Boolean justDrop) at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) GenerateSchema_Fixture.vb(19,0): at ECEHire.Test.Tests.GenerateSchema_Fixture.Can_generate_schema() --ArgumentException at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at NHibernate.Connection.DriverConnectionProvider.GetConnection() at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action1 scriptAction, Boolean export, Boolean justDrop)

0 passed, 1 failed, 0 skipped, took 3.30 seconds (NUnit 2.5.10).

我正在考虑的方向是:web.config 文件在我的主项目中,而不是在测试项目中。这是否意味着我必须为 NHibernate 使用单独的配置文件。如果是这样,我仍然有同样的问题(我仍然得到同样的错误)。

4

1 回答 1

0

是的,每个环境都应该有不同的配置文件:testing 和 web.xml。在测试配置中 current_session_context_class 不应该是 web(而是尝试使用 ThreadStaticSessionContext)。

于 2012-04-24T20:56:42.073 回答