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 节点内加入:
<section name="hibernate-configuration"
                 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>

4. 在数据库里新建一表 NHUser :


5. 新建 NHUser.CS
NHUser.CS
using System;

namespace NHTest
{
    
public class NHUser
    {
        
public NHUser() { }
        
public virtual int UserId
        { 
getset; }

        
public virtual string UserName
        { 
getset; }

        
public virtual string NickName
        { 
getset; }

        
public virtual string UserPass
        { 
getset; }

        
public virtual DateTime regTime
        { 
getset; }

        
public virtual string regIp
        { 
getset; }

    }
}


6. 在站点下新建 NHTest.NHUser.hbm.xml 并作为嵌入的资源编译:
代码
<?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>

注: 由于 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();
        }

经测试,运行正常。


后记: 此文是我几乎是每一步都出现异常后一个一个地查找相关资料才调通。权作记录 NH 学习的开始。
原文地址:https://www.cnblogs.com/infozero/p/1639489.html