- NHibernate 教程
- NHibernate - 首頁
- NHibernate - 概述
- NHibernate - 架構
- NHibernate - ORM
- NHibernate - 環境設定
- NHibernate - 快速入門
- NHibernate - 基本ORM
- NHibernate - 基本CRUD操作
- NHibernate - 效能分析器
- 為對映檔案新增IntelliSense
- NHibernate - 資料型別對映
- NHibernate - 配置
- NHibernate - 覆蓋配置
- NHibernate - 批處理大小
- NHibernate - 快取
- NHibernate - 元件對映
- NHibernate - 關係對映
- NHibernate - 集合對映
- NHibernate - 級聯操作
- NHibernate - 延遲載入
- NHibernate - 反向關係
- NHibernate - Load/Get
- NHibernate - LINQ
- NHibernate - 查詢語言 (HQL)
- NHibernate - Criteria 查詢
- NHibernate - QueryOver 查詢
- NHibernate - 原生SQL
- NHibernate - Fluent NHibernate
- NHibernate 有用資源
- NHibernate - 快速指南
- NHibernate - 有用資源
- NHibernate - 討論
NHibernate - 配置
本章我們將介紹 NHibernate 的配置。我們可以透過不同的方式配置 NHibernate,主要分為兩類:
- 基於XML的配置
- 基於程式碼的配置
基於程式碼的配置
基於程式碼的配置是 NHibernate 內建的功能。它在 NHibernate 3 左右引入,我們一直以來都在使用基於程式碼的配置。
String Data Source = asia13797\\sqlexpress;
String Initial Catalog = NHibernateDemoDB;
String Integrated Security = True;
String Connect Timeout = 15;
String Encrypt = False;
String TrustServerCertificate = False;
String ApplicationIntent = ReadWrite;
String MultiSubnetFailover = False;
cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source +
Initial Catalog + Integrated Security + Connect Timeout + Encrypt +
TrustServerCertificate + ApplicationIntent + MultiSubnetFailover";
x.Driver<SqlClientDriver>();
x.Dialect<MsSql2008Dialect>();
x.LogSqlInConsole = true;
});
cfg.AddAssembly(Assembly.GetExecutingAssembly());
所有配置都在 C# 程式碼中指定。在這裡您可以看到我們建立了一個新的配置物件,然後我們使用在 NHibernate 3.1 中引入的**流暢配置 (loquacious configuration)** 來配置資料庫:我們使用哪個連線字串,連線到哪個資料庫以及要使用的方言。我們還直接在這裡添加了我們的對映程式集。
基於XML的配置
如果您使用基於 XML 的配置,可以使用一個**hibernate.cfg.xml**檔案,它只是一個使用 NHibernate 架構的獨立 XML 檔案,或者您可以將 NHibernate 特定的配置嵌入到您的應用程式或**web.cfg**中。hibernate.cfg.xml 是預設名稱,但我們也可以為該 XML 檔案使用任意名稱。
讓我們透過向 NHibernateDemoApp 專案新增一個名為 hibernate.cfg.xml 的新 XML 檔案來了解基於 XML 的配置。
將以下資訊輸入 hibernate.cfg.xml 檔案。
<?xml version = "1.0" encoding = "utf-8" ?>
<hibernate-configuration xmlns = "urn:nhibernate-configuration-2.2">
<session-factory>
<property name = "connection.connection_string">
Data Source = asia13797\\sqlexpress;
Initial Catalog = NHibernateDemoDB;
Integrated Security = True;
Connect Timeout = 15;
Encrypt = False;
TrustServerCertificate = False;
ApplicationIntent = ReadWrite;
MultiSubnetFailover = False;
</property>
<property name = "connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name = "dialect">
NHibernate.Dialect.MsSql2008Dialect
</property>
<mapping assembly = "NHibernateDemoApp"/>
</session-factory>
</hibernate-configuration>
正如您在上面的 XML 檔案中看到的,我們指定了與 C# 中提到的相同的配置。
現在,讓我們註釋掉 Program.cs 檔案中的配置,只調用**Configure()**方法,它將載入**hibernate.cfg.xml**檔案,如下所示。
using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using System;
using System.Linq;
using System.Reflection;
namespace NHibernateDemoApp {
class Program {
static void Main(string[] args) {
NHibernateProfiler.Initialize();
var cfg = new Configuration();
//cfg.DataBaseIntegration(x =>
//{
// x.ConnectionString = "Data Source = asia13797;\\sqlexpress
Initial Catalog = NHibernateDemoDB;
Integrated Security = True;
Connect Timeout = 15;
Encrypt =False;
TrustServerCertificate = False;
ApplicationIntent = ReadWrite;
MultiSubnetFailover = False";
// x.Driver<SqlClientDriver>();
// x.Dialect<MsSql2008Dialect>();
// x.LogSqlInConsole = true;
//});
//cfg.AddAssembly(Assembly.GetExecutingAssembly());
cfg.Configure();
var sefact = cfg.BuildSessionFactory();
using (var session = sefact.OpenSession()) {
using (var tx = session.BeginTransaction()) {
var students = session.CreateCriteria<Student>().List<Student>();
Console.WriteLine("\nFetch the complete list again\n");
foreach (var student in students) {
Console.WriteLine("{0} \t{1} \t{2} \t{3}", student.ID,
student.FirstName, student.LastName, student.AcademicStanding);
}
tx.Commit();
}
Console.ReadLine();
}
}
}
}
讓我們再次執行您的應用程式,您將看到相同的輸出。
Fetch the complete list again 1 Allan Bommer Excellent 2 Jerry Lewis Good
廣告