关于如何在ASP.NET2.0网站与Visual Web Developer Express中配置NHibernate的问题
在ASP.NET项目中使用NHibernate老是会碰到无法加载配置文件的问题,刚才瞎折腾了1小时,才从外国的一篇博客里找到了解决方案.地址如下:http://derek-says.blogspot.com/2008/02/nhibernate-in-visual-web-developer.html.
现在我来简单的讲解下如何配置.
1.数据库里建表,如tb_Department
2.然后编写对应的类文件, 如我对应类的全名是:NetFuture.NHibernateClass.NH_tb_Department
3.在WebConfig中加入配置,一下是我的配置文件:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2000Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=(local);Integrated Security=True; initial catalog=DB_PersonnelManageSystem
</property>
<mapping assembly="App_Code"/>
</session-factory>
</hibernate-configuration>
红色的那句貌似是关键的地方.
4.编写类对应的映射文件,如:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="NetFuture.NHibernateClass.NH_tb_Department, App_Code" table="tb_Department" >
<id name="DepartmentId" column="DepartmentId" type="Int32" length="4" unsaved-value="0">
<generator class="native" />
</id>
<property name="DepartmentName" column="DepartmentName" type="String" length="16" />
<property name="Description" column="Description" type="String" length="200" />
<property name="ManagerId" column="ManagerId" type="String" length="15" />
</class>
</hibernate-mapping>
红色的那句也是关键的地方.
5.将类与映射文件都放置与App_Code文件夹下
6.编写类似下面的NHibernate初始化配置代码:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.AddDirectory(new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(@"~/App_Code/")));
_factory = cfg.BuildSessionFactory();
这样就完成了对App_Code文件夹的映射,NHibernate就会在其中搜索配置文件了.
第一次写随笔,希望能对你有所帮助^_^.