c#,NHibernate,ASP.NET2.0,Winform

None.gif
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  Model
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Person#region Person
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Person object for NHibernate mapped table 'Person'.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Person 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Member Variables#region Member Variables
InBlock.gif        
InBlock.gif        
protected int _id;
InBlock.gif        
protected string _name;
InBlock.gif        
protected static String _sortExpression = "Id";
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructors#region Constructors
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Person() dot.gif{ }
InBlock.gif
InBlock.gif        
public Person( string name )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._name = name;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Public Properties#region Public Properties
InBlock.gif
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _id;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_id = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _name; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null && value.Length > 50)
InBlock.gif                    
throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
InBlock.gif                _name 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static String SortExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sortExpression; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _sortExpression = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IComparable Methods#region IComparable Methods
InBlock.gif       
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2" >
None.gif    
< class  name ="Model.Person, Model"  table ="Person"  lazy ="false" >
None.gif        
< id  name ="Id"  type ="Int32"  unsaved-value ="null" >
None.gif            
< column  name ="id"  length ="4"  sql-type ="int"  not-null ="true"  unique ="true"  index ="PK_Person" />
None.gif            
< generator  class ="native"   />
None.gif        
</ id >
None.gif        
< property  name ="Name"  type ="String" >
None.gif            
< column  name ="name"  length ="50"  sql-type ="varchar"  not-null ="false" />
None.gif        
</ property >
None.gif    
</ class >
None.gif
</ hibernate-mapping >
None.gif
以上两个文件都是用codesmith自动生成的,很好用,里面有NHibernate模板。

有三个注意事项:
1、提示什么"Unknown entity class"之类的
请注意映射文件的<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">,一定要是2.2
2、
 如果提示实体类的属性需要加上virtual关键字,就需要在<class name="Model.Person, Model" table="Person" lazy="false">中加上 lazy="false"
3、
提示什么"Unknown entity class"之类的
右键实体类对应的Person.hbm.xml需要作为嵌入资源,右键文件,属性,Build Action设置为嵌入资源

下面是web.config文件中的内容,指明数据库的位置和类型,如果提示数据库没有打开远程访问之类的信息,需要在sqlserver 外围应用配置器中中打开本地连接和远程连接,同时启用命名管道和TCP/IP,在sqlserver configuration manager的sql2005协议将tcp/ip双击打开,将IP地址tab中的IPALL的TCP动态端口改为默认的1433.
None.gif   < configSections >
None.gif      
< section  name ="nhibernate"  type ="System.Configuration.NameValueSectionHandler, System,
None.gif                    Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
  />
None.gif    
</ configSections >
None.gif
None.gif    
< nhibernate >
None.gif      
< add  key ="hibernate.connection.provider"  value ="NHibernate.Connection.DriverConnectionProvider"   />
None.gif      
< add  key ="hibernate.connection.driver_class"  value ="NHibernate.Driver.SqlClientDriver"   />
None.gif      
< add  key ="hibernate.connection.connection_string"  value ="server=127.0.0.1;uid=virus;pwd=123.com;database=TestDB"   />
None.gif      
< add  key ="hibernate.connection.isolation"  value ="ReadCommitted" />
None.gif      
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2005Dialect"   />
None.gif    
</ nhibernate >

下面是c#的代码

None.gif NHibernate.Cfg.Configuration config  =   new  NHibernate.Cfg.Configuration().AddAssembly( " Model " );
None.gif        ISessionFactory factory 
=  config.BuildSessionFactory();
None.gif        ISession session 
=  factory.OpenSession();
None.gif
None.gif        Person person 
=   new  Person();
None.gif        person.Name 
=   " swb " ;
None.gif
None.gif        ITransaction tran 
=  session.BeginTransaction();
None.gif        
try
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            session.Save(person);
InBlock.gif            tran.Commit();
InBlock.gif            Response.Write(
"this is ok.");
ExpandedBlockEnd.gif        }

None.gif        
catch  (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            tran.Rollback();
InBlock.gif            Response.Write(ex.Message);
ExpandedBlockEnd.gif        }

None.gif        
finally
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            session.Close();
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值