1 App.config 設定:
<?XML Version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" requirePermission="false"/> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider,NHibernate</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <プロパティ名="connection.connection_string"> <!--Server=(local); 初期カタログ=nhibernate; 統合セキュリティ=SSPI--> database='nhibernate'; server=.; uid='sa'; PWD='SA'; </property> <プロパティ名="adonet.batch_size">10</property> <プロパティ名="show_sql">true</property> <プロパティ名="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <プロパティ名="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, nibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> ----------------------------------------------------------------------------------------------------------------------
#region 2。 App.config 設定 Configuration config = new Configuration(). AddAssembly("Test.Model");
factory = config。 BuildSessionFactory(); セッション=工場。 OpenSession(); #endregion
-------------------------------------------------------------------------------------------------------------------------- </configuration>
2 Web.config Configuration 配置するバイト位置に注意
<!--ハブネート―-> <セクション名="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> <!--ハイバーネート――> </configSections> <!--ハイバーネート――> <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> <プロパティ名="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <プロパティ名="connection.connection_string">database='nhibernate'; server=.; uid='sa'; PWD='SA'; </property> <プロパティ名="show_sql">true</property> <プロパティ名="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, nibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> <!--ハイバーネート――> <appSettings/>
---------------------------------------------------------------------------------------------------------
#region 2. web.configを使って設定してください。
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(). AddAssembly("Test.Model"); factory = config。 BuildSessionFactory(); セッション=工場。 OpenSession();
#endregion
-----------------------------------------------------------------------------------------------------------
3 NHibernate.cfg.xml構成:
<?XML Version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-mapping-2.2"> <session-factory xmlns="urn:nhibernate-configuration-2.2"> <!--性質 --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <プロパティ名="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <プロパティ名="connection.connection_string">database='nhibernate'; server=.; uid='sa'; PWD='SA'; </property> <プロパティ名="show_sql">true</property> <プロパティ名="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, nibernate.ByteCode.LinFu</property> <プロパティ名="use_outer_join">true</property> <!--マッピングファイル --> <mapping assembly="Test.Model" /> </session-factory> </hibernate-configuration>
-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------
#region 3. NHibernate.cfg.xml構成を活用する
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(); 設定。 Configure(@"C:\Documents and Settings\Administrator\Desktop\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml"); factory = config。 BuildSessionFactory(); セッション=工場。 OpenSession(); #endregion
4 MyConfiguration.cs構造を次のように作成します:
public static NHibernate.Cfg.Configuration GetConfig() {
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(); 設定。 SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider"); 設定。 SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver"); 設定。 SetProperty("hibernate.connection.connection_string", "database='nhibernate'; server=.; uid='sa'; PWD='SA'; "); 設定。 SetProperty("hibernate.dialect", "nibernate.Dialect.MsSql2005Dialect, NHibernate"); 設定。 SetProperty("hibernate.use_outer_join"、「true」); 設定。 SetProperty("hibernate.show_sql", "false"); 設定。 SetProperty("proxyfactory.factory_class", "nibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"); 設定。 AddAssembly("Test.Model"); 設定を返す;
}
------------------------------------------------------------------------------------------------------------------------------
#region 4. MyConfiguration.cs構成を使う
NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Use MyConfiguration.cs factory = config。 BuildSessionFactory(); factory = MyConfiguration.GetConfig(). BuildSessionFactory(); セッション=工場。 OpenSession(); #endregion |