1.配置
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
<!--Server=localhost;Initial Catalog=NHibernate;Integrated Security=SSPI-->
<!--server=(local);database=NHibernate;uid=sa;pwd=sa;Pooling=true;Max Pool Size=100;Min Pool Size=1-->
server=PC-201011100005\SQLEXPRESS;database=NHibernate;uid=sa;pwd=sa1;Pooling=true;Max Pool Size=100;Min Pool Size=1
</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="test.Model"/>
</session-factory>
</hibernate-configuration>
2.*.hbm.xml改为嵌入的资源
3.调用
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().Configure();
//通过配置对象来产生一个SessionFactory对象,这是一个Session工厂,
//那么Session是用来干什么的呢?一个Session就是由NHibernate封装
//的工作单元,我们可以近似地认为它起到ADO.Net中Connection的作用。
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
//这里,开启一个由NHibernate封装的事务,当然,在这里最终代表
//的还是一个真实的数据库事务,但是我们已经不需要再区分到底是
ITransaction trans = session.BeginTransaction();
Person person = new Person();
person.Name = "Jackie Chan";
try
{
session.Save(person);
trans.Commit();
Response.Write("Insert Success!");
}
catch (Exception ex)
{
trans.Rollback();
Response.Write(ex.Message);
}