gpt4 book ai didi

c# - 在 ASP.NET 4.0 中通过 Web.config 配置 NHibernate

转载 作者:太空狗 更新时间:2023-10-30 00:46:31 25 4
gpt4 key购买 nike

所以我的单元测试是绿色的,是时候将这个 Shiny 的新 NHibernate 驱动的 DAL 集成到我的网络应用程序中了!我真的不想维护两个配置文件,所以我将 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 中,dummy!”任何地方?

然后当我按下 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 中指定了这个属性……我想知道可能发生了什么?

那么,有人有什么想法吗?解决这个谜团的任何帮助都是 super 的!

*更新:我发现了这个问题。看起来我在配置部分没有使用正确的类型!哦。 I have a complete write up on my blog .

最佳答案

尝试在最后调用 .Configure() 方法:

new Configuration().AddAssembly("MyProject").Configure();

或者,如果您更喜欢将其放入 web.config 中:

<nhibernate xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="">
...
<mapping assembly="MyProject" />
</session-factory>
</nhibernate>

然后:

new Configuration().Configure();

还要确保在您的 Web 项目中引用了 NHibernate.ByteCode.CaSTLe.dll 程序集。

关于c# - 在 ASP.NET 4.0 中通过 Web.config 配置 NHibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com