初学NHibernate1.2.0.4000注意事项

本来刚刚初学,遇到一些问题罗列一下,给我一样的初学者参考一下。
请先下载 NHibernate源码,并添加 Application1项目(控制台应用程序)
第一步 建表(SQL语句)
use  dbtemp
go

CREATE   TABLE  users (
  LogonID 
nvarchar ( 20 NOT   NULL   default   ' 0 ' ,
  Name 
nvarchar ( 40 default   NULL ,
  Password 
nvarchar ( 20 default   NULL ,
  EmailAddress 
nvarchar ( 40 default   NULL ,
  LastLogon 
datetime   default   NULL ,
  
PRIMARY   KEY   (LogonID)
)
go

第二步 建实体类 User.cs
using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Application1
{
    
public   class  User
    {
        
private   string  id;
        
private   string  userName;
        
private   string  password;
        
private   string  emailAddress;
        
private  DateTime lastLogon;


        
public  User()
        {
        }

        
public   virtual   string  Id
        {
            
get  {  return  id; }
            
set  { id  =  value; }
        }

        
public   virtual   string  UserName
        {
            
get  {  return  userName; }
            
set  { userName  =  value; }
        }

        
public   virtual   string  Password
        {
            
get  {  return  password; }
            
set  { password  =  value; }
        }

        
public   virtual   string  EmailAddress
        {
            
get  {  return  emailAddress; }
            
set  { emailAddress  =  value; }
        }

        
public   virtual  DateTime LastLogon
        {
            
get  {  return  lastLogon; }
            
set  { lastLogon  =  value; }
        }

    }
}

第三步写映射文件 User.hbm.xml
<? xml version="1.0" encoding="utf-8"  ?>
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2" >
  
< class  name ="Application1.User, Application1"  table ="users" >
    
< id  name ="Id"  column ="LogonId"  type ="String"  length ="20" >
      
< generator  class ="assigned"   />
    
</ id >
    
< property  name ="UserName"  column ="Name"  type ="String"  length ="40" />
    
< property  name ="Password"  type ="String"  length ="20" />
    
< property  name ="EmailAddress"  type ="String"  length ="40" />
    
< property  name ="LastLogon"  type ="DateTime" />
  
</ class >
</ hibernate-mapping >

第四步建配置文件 App.config
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
  
< configSections >
    
< section
      
name ="nhibernate"
      type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    
/>
  
</ configSections >

  
< nhibernate >
    
< add
      
key ="hibernate.connection.provider"
      value
="NHibernate.Connection.DriverConnectionProvider"
    
/>
    
< add
      
key ="hibernate.dialect"
      value
="NHibernate.Dialect.MsSql2000Dialect"
    
/>
    
< add
      
key ="hibernate.connection.driver_class"
      value
="NHibernate.Driver.SqlClientDriver"
    
/>
    
< add
      
key ="hibernate.connection.connection_string"
      value
="Server=localhost;initial catalog=tempdb;Integrated Security=SSPI"
    
/>
  
</ nhibernate >
</ configuration >

最后一步就可以写运行的 Program.cs
using  System;
using  System.Collections.Generic;
using  System.Text;
using  NHibernate.Cfg;
using  NHibernate;

namespace  Application1
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Configuration cfg 
=   new  Configuration();
            cfg.AddAssembly(
" Application1 " );

            ISessionFactory factory 
=  cfg.BuildSessionFactory();
            ISession session 
=  factory.OpenSession();
            ITransaction transaction 
=  session.BeginTransaction();

            User newUser 
=   new  User();
            newUser.Id 
=   " joe_cool " ;
            newUser.UserName 
=   " Joseph Cool " ;
            newUser.Password 
=   " abc123 " ;
            newUser.EmailAddress 
=   " joe@cool.com " ;
            newUser.LastLogon 
=  DateTime.Now;

            session.Save(newUser);

            transaction.Commit();
            session.Close();

        }
    }
}

这样你就可以运行了。
参考 http://www.hibernate.org/362.html
在这里的示例代码你要注意几点:
1.User.hbm.xml这个文件是 嵌入的资源的
2.User.hbm.xml文件中
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">要改成<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
3.User.cs文件中前面要加virtual修饰
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值