一.建好后添加引用:
1。 NHibernate.dll
2。 log4net.dll
3。 nunit.framework.dll (在安装好的软件的目录里/bin/nunit.framework.dll)
4。 还有自己写好 Model.dll (实体类映射文件和)
二.现在手添加一个XML 文件
(在 NHibernate用log4net和NUnit调试 1 然后是添加一个应用程序配置文件)
//-----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="sampledb">
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local);Database=test;User ID=sa;Password=zjjchy19841028;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">test.dbo</property>
<property name="show_sql">true</property>
<!--<property name="hibernate.current_session_context_class">NHibernate.Context.ManagedWebSessionContext</property>-->
<!-- HBM Mapping Files -->
<mapping assembly="Model" />
</session-factory>
</hibernate-configuration>
三. 测试类
//-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NUnit.Framework;
using Model;
namespace ClassLibrary
{
[TestFixture]
[Category("Composite")]
public class CassExample : NhSession
{
[Test]
public void SaveAll()
{
Bookinfo i = new Bookinfo();
i.Id = "test2";
i.BookInfo = "chyinfoss";
ITransaction trans = Session.BeginTransaction();
Session.Save(i);
trans.Commit();
}
}
}
四.运行程序
本文详细介绍如何使用NHibernate进行数据库操作,包括配置环境、添加引用、创建配置文件,并通过一个具体的测试类实例演示如何保存数据。


359

被折叠的 条评论
为什么被折叠?



