我在设置NHibernate配置时遇到问题。如果我创建一个单独的配置文件,我可以让它工作,然后当我调用Configuration.configure时,我指向配置文件的绝对路径(“C:\ My Documents ... etc ...”)。这显然会当我尝试从开发转移到生产时出现问题。所以我想我会将配置移到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.
----&GT; System.ArgumentException:初始化字符串的格式不符合从索引49开始的规范。
at 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(Action
1 scriptAction,Boolean export,Boolean justDrop)
0 passed, 1 failed, 0 skipped, took 3.30 seconds (NUnit 2.5.10).
我正在考虑以下方向:web.config文件位于我的主项目中,而不是测试项目。这是否意味着我必须为NHibernate使用单独的配置文件。如果是这样,我仍然有同样的问题(我仍然得到相同的错误)。
答案 0 :(得分:0)
是的,您应该为每个环境配置不同的配置文件:testing和web。在测试配置中,current_session_context_class不应该是web(而是尝试使用ThreadStaticSessionContext)。