1. Library檔案
需要的dll檔案如下:
FluentNHibernate.dll (*)
NHibernate.ByteCode.Castle.dll (*)
NHibernate.dll (*)
Remotion.Data.Linq.dll
Antlr3.Runtime.dll
Castle.Core.dll
Iesi.Collections.dll
2. 建立Hibernate通路通用類
using System;
using System.Collections.Generic;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;
using System.Linq;
namespace CsharpTrainer.NHibernate3
{
public class NHibernateDb<TMap> : IDisposable
{
private string connString;
private ISessionFactory sessFactory;
private ISession session;
public ISessionFactory SessionFactory
{
get { return sessFactory; }
set { sessFactory = value; }
}
public ISession Session
get { return session; }
set { session = value; }
public void Dispose()
session.Dispose();
public NHibernateDb(string pStr)
connString = pStr;
sessFactory = CreateSessionFactory();
session = sessFactory.OpenSession();
~NHibernateDb()
try
{
Dispose();
}
catch
Console.WriteLine("Exception: session dispose failed!");
private ISessionFactory CreateSessionFactory()
return Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008
.ConnectionString(connString))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<TMap>())
.BuildSessionFactory();
private void CreateSchema(Configuration cfg)
var schemaExport = new SchemaExport(cfg);
schemaExport.Drop(false, true);
schemaExport.Create(false, true);
public void CreateDatabase()
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(connString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TMap>())
.ExposeConfiguration(CreateSchema)
.BuildConfiguration();
}
}
3. 建立表格對應的實體類
我們以Northwind資料庫的Employees表格為例
namespace CsharpTrainer.NHibernate3.Entities
public class Employees
public virtual int EmployeeID { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual string Title { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string Region { get; set; }
public virtual string Country { get; set; }
public virtual string Notes { get; set; }
public override string ToString()
string format = "Employee ID: {0}\nLast Name: {1}\n"
+ "First Name: {2}\nTitle: {3}\nBirth Date: {4}\n"
+ "Address: {5}\nCity: {6}\nRegion: {7}\nCountry: {8}\n"
+ "Notes: {9}\n";
return string.Format(format, EmployeeID, LastName, FirstName,
Title, BirthDate, Address, City, Region, Country, Notes);
4. 建立映射關系(映射類)
EmployeeMap Class:
using FluentNHibernate.Mapping;
using CsharpTrainer.NHibernate3.Entities;
namespace CsharpTrainer.NHibernate3.Mapping
public class EmployeeMap : ClassMap<Employees>
public EmployeeMap()
Id(x => x.EmployeeID, "EmployeeID");
Map(x => x.LastName)
.Length(20)
.Not.Nullable();
Map(x => x.FirstName)
.Length(10)
Map(x => x.Title)
.Length(30);
Map(x => x.BirthDate);
Map(x => x.Address)
.Length(60);
Map(x => x.City)
.Length(15);
Map(x => x.Region)
Map(x => x.Country)
Map(x => x.Notes);
5. 用戶端查詢代碼
查詢所有的員工,按照LastName排序
public void Run()
string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString;
NHibernateDb<EmployeeMap> db = new NHibernateDb<EmployeeMap>(connStr);
ISessionFactory factory = db.SessionFactory;
using (var session = factory.OpenSession())
{
var employees = session.QueryOver<Employees>()
.OrderBy(a => a.LastName).Asc
.List();
Console.WriteLine("All Employees Below:");
foreach (var employee in employees)
{
Console.WriteLine(employee);
}
}
catch (Exception ex)
Console.WriteLine("Exception: " + ex.ToString());