Nhibernate配置映射文件
本文关键字:文件 映射 配置 Nhibernate | 更新日期: 2023-09-27 18:24:02
我有一个web项目,使用nhibernate配置文件,如下所示:
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=.'sqlexpress;Initial Catalog=afemanager;Integrated Security=no;User=sa;Password=password;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="show_sql">true</property>
<mapping file="afe-serialization.hbm.xml"/>
<mapping file="afe-view.hbm.xml"/>
</session-factory>
然后,我读到这样的配置:
public static ISession GetSession()
{
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
return config.Configure(Path.Combine( HttpContext.Current.Server.MapPath( "/" ), "App_Data", NHIBERNATE_CFG)).BuildSessionFactory().OpenSession();
}
当它运行时,它的显示异常:
"/"应用程序中的服务器错误。找不到文件"C:''Program Files"(x86)''通用文件''MicrosoftShared''DevServer''11.0''afe序列化.hbm.xml'.
我的.hbm文件不在该目录中。我的问题是如何设置映射文件以从App_Data目录中获取.hbm文件。
类似这样的东西:
Path.Combine(HttpContext.Current.Server.MapPath("/"), "App_Data", "afe-serialization.hbm.xml")
您可能需要将hbm.xml文件的"Build Action"属性设置为"Embedded Resources",以便在部署期间将其复制到您的项目工作目录中。您可以使用属性窗口执行此操作。在这种情况下,映射文件可以位于您选择的任何目录中。
你的代码可以是这样的:
public static ISession GetSession()
{
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
return config.AddAssembly("Assembly Name").BuildSessionFactory().OpenSession();
}
希望这能有所帮助!
如果使用
config.Configure();
如果没有参数,它将从bin文件夹加载默认的hibernate.hbm.xml。