NHibernate 学习笔记
最近开始接触 NHibernate , 初次使用曲折颇多,最后终于顺利调通。特将过程记录下来,希望对后来者有帮助(我的开发环境是 :Vs2008SP1 + MSSQL 2000)。
1. 先下载 NHibernate 框架, 我用的是 2.1 版;
2. 新建一 Asp.net Web 应用程序 NHTest ,并添加引用:
NHibernate.dll ,NHibernate.ByteCode.LinFu.dll ,LinFu.DynamicProxy.dll
3. 修改 Web.config 以配置 NHibernate 。在 configSections 节点内加入:
4. 在数据库里新建一表 NHUser :
5. 新建 NHUser.CS
1. 先下载 NHibernate 框架, 我用的是 2.1 版;
2. 新建一 Asp.net Web 应用程序 NHTest ,并添加引用:
NHibernate.dll ,NHibernate.ByteCode.LinFu.dll ,LinFu.DynamicProxy.dll
3. 修改 Web.config 以配置 NHibernate 。在 configSections 节点内加入:
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
然后在 与 configSections 平级的节点上增加:
代码
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHTest">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=127.0.0.1; uid=sa; pwd=123456; database=testcenter
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHTest" />
</session-factory>
</hibernate-configuration>
<session-factory name="NHTest">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=127.0.0.1; uid=sa; pwd=123456; database=testcenter
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHTest" />
</session-factory>
</hibernate-configuration>
4. 在数据库里新建一表 NHUser :
5. 新建 NHUser.CS
NHUser.CS
using System;
namespace NHTest
{
public class NHUser
{
public NHUser() { }
public virtual int UserId
{ get; set; }
public virtual string UserName
{ get; set; }
public virtual string NickName
{ get; set; }
public virtual string UserPass
{ get; set; }
public virtual DateTime regTime
{ get; set; }
public virtual string regIp
{ get; set; }
}
}
namespace NHTest
{
public class NHUser
{
public NHUser() { }
public virtual int UserId
{ get; set; }
public virtual string UserName
{ get; set; }
public virtual string NickName
{ get; set; }
public virtual string UserPass
{ get; set; }
public virtual DateTime regTime
{ get; set; }
public virtual string regIp
{ get; set; }
}
}
代码
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="NHTest.NHUser,NHTest" table="NHUser">
<id name="UserId" column ="UserId">
<generator class="native"/>
</id>
<property name ="UserName"/>
<property name ="NickName"/>
<property name ="UserPass"/>
<property name ="regTime"/>
<property name ="regIp"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="NHTest.NHUser,NHTest" table="NHUser">
<id name="UserId" column ="UserId">
<generator class="native"/>
</id>
<property name ="UserName"/>
<property name ="NickName"/>
<property name ="UserPass"/>
<property name ="regTime"/>
<property name ="regIp"/>
</class>
</hibernate-mapping>
注: 由于 UserId 在数据库里设置的是自增长 ID ,这里的 <generator class="native"/> 要设置为 native, 否则会引发 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 "NH_User" 中的标识列插入显式值 的异常。
下面就是如何使用了。在 default.aspx 页面中拖下几个控件:
后台 CS 代码 ,引用命名空间 :
using NHibernate;
using NHibernate.Cfg;
default.cs
private void AddUser() {
Configuration cfg = new Configuration();
cfg.AddAssembly("NHTest");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction trans = session.BeginTransaction();
NHUser u = new NHTest.NHUser()
{
NickName = tbnick.Text,
UserName = tbuname.Text,
UserPass = tbpass.Text,
regTime = DateTime.Now,
regIp = Request.UserHostAddress
};
session.Save(u);
session.Flush();
trans.Commit();
}
protected void btnOK_Click(object sender, EventArgs e)
{
AddUser();
}
Configuration cfg = new Configuration();
cfg.AddAssembly("NHTest");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction trans = session.BeginTransaction();
NHUser u = new NHTest.NHUser()
{
NickName = tbnick.Text,
UserName = tbuname.Text,
UserPass = tbpass.Text,
regTime = DateTime.Now,
regIp = Request.UserHostAddress
};
session.Save(u);
session.Flush();
trans.Commit();
}
protected void btnOK_Click(object sender, EventArgs e)
{
AddUser();
}
经测试,运行正常。