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 平级的节点上增加:

ExpandedBlockStart.gif 代码
   < 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
ExpandedBlockStart.gif 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 ; }

    }
}


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

ExpandedBlockStart.gif 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/archive/2010/01/05/1639489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值