1.設定configSections區段
<configSections>2.設定hibernate-configuration區段
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
(a) SQL Server 2008
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">(b) Oracle 10g
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string">Data Source=10.0.0.10;Initial Catalog=dbname;Persist Security Info=True;User ID=username;Password=password;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHibernateTest.Data"/>
</session-factory>
</hibernate-configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">(c) PostgreSQL 8.4
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="connection.connection_string">Data Source=XXX;Persist Security Info=True;User ID=username;Password=password;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHibernateTest.Data"/>
</session-factory>
</hibernate-configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">也可將連線字串的設定移到connectionStrings區段中,如以下範例
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
<property name="connection.connection_string">Server=10.0.0.10;Port=5432;Database=dbname;User ID=username;Password=password;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHibernateTest.Data"/>
</session-factory>
</hibernate-configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">備註
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
<property name="connection.connection_string_name">PostgreSQL</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="NHibernateTest.Data"/>
</session-factory>
</hibernate-configuration>
<connectionStrings>
<add name="PostgreSQL" connectionString="Server=10.0.0.10;Port=5432;Database=dbname;User ID=username;Password=password;"/>
</connectionStrings>
1.筆者測試用的PostgreSQL版本為8.4,但在NHibernate 2.1.2支援的dialect只到8.2,可設定的dialect可參考http://nhforge.org/doc/nh/en/index.html#configuration-optional-dialects
2.<mapping assembly="NHibernateTest.Data"/>為對應到資料庫所使用的domain entities編譯檔