November 30, 2010

NHibernate - 設定檔範例 for SQL Server/Oracle/PostgreSQL

以下提供SQL Server  2008Oracle 10gPostgreSQL 8.4的設定檔範例,需設定在Web.config/App.config
1.設定configSections區段
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    </configSections> 
2.設定hibernate-configuration區段
(a) SQL Server 2008
     <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.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>
(b) Oracle 10g
    <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.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>
(c) PostgreSQL 8.4
    <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">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>
也可將連線字串的設定移到connectionStrings區段中,如以下範例
     <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編譯檔

No comments: