所以我的單元測試是綠色的,有時候將這個閃亮的新NHibernate驅動的DAL集成到我的web應用程序中!我不想維護兩個配置文件,所以我已經將hibernate.cfg.xml遷移到了我的Web.config文件中(即,我將hibernate.cfg.xml的內容複製到了我的Web.config中)。下面是我的web.config中的相關位:在ASP.NET 4.0中通過Web.config配置NHibernate
<configSections>
<section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<nhibernate xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local)\SQLExpress;Initial Catalog=MyProject;Integrated Security=True</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<listener class="MyProject.Sql.Listeners.SaveEventListener, MyProject" type="save"/>
<listener class="MyProject.Sql.Listeners.UpdateEventListener, MyProject" type="update"/>
</session-factory>
</nhibernate>
在Global.asax中,上的Application_Start,我嘗試初始化我的配置:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
SessionProvider.Initialize();
}
這一切確實是呼叫new Configuration().Configure().AddAssembly("MyProject");
按照上面的配置代碼。
有趣的結果:當我第一次打到頁面,則拋出異常:
[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.]
好吧,我把Web.config中的配置,應該不是在那裏lookign?我需要指出「嘿,NHibernate,注意 - 配置數據在Web.config中,虛擬!」地方?
當我點擊F5時,頁面出現。歡呼!現在,我嘗試做與數據訪問的東西,我得到這個異常:
[ProxyFactoryFactoryNotConfiguredException: The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
Example:
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
Example:
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>]
咦,這是有點奇怪太 - 這工作就好了測試與hibernate.cfg.xml配置...和我在我的Web.config中指定這個屬性...我想知道什麼可能會起來?
那麼,任何人有任何想法?解決這個謎題的任何幫助都是超級的!
*更新:我找到了問題。它看起來像我沒有在我的配置部分使用正確的類型! D'哦。 I have a complete write up on my blog。
非常有用。非常感謝neebur。 – Simian 2012-02-08 10:21:46