【发布时间】:2011-03-06 16:01:03
【问题描述】:
所以我的单元测试是绿色的,是时候将这个闪亮的新 NHibernate 驱动的 DAL 集成到我的 Web 应用程序中了!我真的不想维护两个配置文件,所以我已将 hibernate.cfg.xml 迁移到我的 Web.config 文件中(即我将 hibernate.cfg.xml 的内容复制粘贴到我的 Web.config 中)。以下是我的 Web.config 中的相关位:
<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里,不应该在那里看看吗?我是否需要指出“嘿,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 中指定了这个属性......我想知道可能会发生什么?
那么,有人有什么想法吗?解开这个谜团的任何帮助都会非常棒!
*更新:我发现了问题。看起来我没有在我的配置部分使用正确的类型!哦。 I have a complete write up on my blog.
【问题讨论】:
标签: c# nhibernate configuration web-config