1. 应用层的工程的App.config也要有NHibernate相关的配置。
使用NHibernate 注意要点<?xml version="1.0" encoding="utf-8" ?>
使用NHibernate 注意要点
<configuration>
使用NHibernate 注意要点  
<configSections>
使用NHibernate 注意要点    
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
使用NHibernate 注意要点  
</configSections>
使用NHibernate 注意要点  
<nhibernate>
使用NHibernate 注意要点    
<add         key="hibernate.connection.provider"       value="NHibernate.Connection.DriverConnectionProvider" 
使用NHibernate 注意要点        
/>
使用NHibernate 注意要点    
<add 
使用NHibernate 注意要点
key="hibernate.dialect"                      
使用NHibernate 注意要点value
="NHibernate.Dialect.MsSql2000Dialect" 
使用NHibernate 注意要点        
/>
使用NHibernate 注意要点    
<add key="hibernate.show_sql" value="false"/>
使用NHibernate 注意要点    
<add 
使用NHibernate 注意要点    
key="hibernate.connection.driver_class"        value="NHibernate.Driver.SqlClientDriver" 
使用NHibernate 注意要点        
/>
使用NHibernate 注意要点    
<add 
使用NHibernate 注意要点            
key="hibernate.connection.connection_string"         value="Server=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=SSPI" 
使用NHibernate 注意要点        
/>
使用NHibernate 注意要点  
</nhibernate>
使用NHibernate 注意要点
</configuration>

2. 数据库设计当中,表名,字段名不能有数据库的关键字,如:user, group.....字段名最好不与表名相同

3.实体映射文件(*.hbm.xml)要设置为Embeded Resource(嵌入式资源)文件。
4.对于one-to-many, 配置要用set, 实体类要用Iesi.Collections.ISet。
例如:
Role.hbm.xml
使用NHibernate 注意要点<?xml version="1.0" encoding="utf-8" ?>
使用NHibernate 注意要点
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
使用NHibernate 注意要点  
<class name="Common.Entity.Role,Common" table="Role">
使用NHibernate 注意要点    
<id name="RoleKey" type="Int32">
使用NHibernate 注意要点      
<generator class="assigned" />
使用NHibernate 注意要点    
</id>
使用NHibernate 注意要点    
<property name="RoleName" type="String"/>
使用NHibernate 注意要点    
<set name="UserMembers" inverse="true" cascade="all">
使用NHibernate 注意要点      
<key column="RoleKey"/>
使用NHibernate 注意要点      
<one-to-many class="Common.Entity.User,Common"/>
使用NHibernate 注意要点    
</set>
使用NHibernate 注意要点  
</class>
使用NHibernate 注意要点
</hibernate-mapping>
Role.cs
使用NHibernate 注意要点 public class Role
    }

5.实体类一定要override Equals方法与GetHashcode方法。

6.对数据进行插入、修改、删除要使用事务:
ITransaction tran=session.BegionTransaction();
//Do something
tran.Commit();

相关文章: