博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Nhibernate 三种配置方式

Posted on 2010-06-15 11:47  linFen  阅读(4220)  评论(1编辑  收藏  举报

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>
      <property name="connection.connection_string">
        <!--Server=(local);initial catalog=nhibernate;Integrated Security=SSPI-->
        database='nhibernate';server=.;uid='sa';pwd='sa';
      </property>
      <property name="adonet.batch_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    </session-factory>
</hibernate-configuration>
----------------------------------------------------------------------------------------------------------------------

#region 2 。 App.config 配置
            Configuration config = new Configuration().AddAssembly("Test.Model");        

        factory = config.BuildSessionFactory();
          session = factory.OpenSession();
#endregion

--------------------------------------------------------------------------------------------------------------------------
</configuration>

2 web.config的配置 注意 所放字节位置

<!--NHibernate -->
   <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
   <!--NHibernate-->
</configSections>
<!--NHibernate-->
<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.MsSql2005Dialect</property>
    <property name="connection.connection_string">database='nhibernate';server=.;uid='sa';pwd='sa';</property>
    <property name="show_sql">true</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
   </session-factory>
</hibernate-configuration>
<!--NHibernate-->
<appSettings/>

---------------------------------------------------------------------------------------------------------

 

#region 2、利用web.config 配置 通过

        //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Test.Model");
        //factory = config.BuildSessionFactory();
        //session = factory.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">
    <!-- properties -->
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.connection_string">database='nhibernate';server=.;uid='sa';pwd='sa';</property>
    <property name="show_sql">true</property>
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="use_outer_join">true</property>
    <!-- mapping files -->
    <mapping assembly="Test.Model" />
</session-factory>
</hibernate-configuration>

-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------

   #region 3、利用NHibernate.cfg.xml配置

        //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
        //config.Configure(@"C:\Documents and Settings\Administrator\桌面\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml");
        //factory = config.BuildSessionFactory();
        //session = factory.OpenSession();
        #endregion

 

4     创建一个MyConfiguration.cs 结构如下:

 

public static NHibernate.Cfg.Configuration GetConfig()
    {

      
        NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
        config.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        config.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        config.SetProperty("hibernate.connection.connection_string", "database='nhibernate';server=.;uid='sa';pwd='sa';");
        config.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate");
        config.SetProperty("hibernate.use_outer_join", "true");
        config.SetProperty("hibernate.show_sql", "false");
        config.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
        config.AddAssembly("Test.Model");
        return config;
     
    }

 

------------------------------------------------------------------------------------------------------------------------------

#region 4、使用MyConfiguration.cs配置

        //NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig();//使用MyConfiguration.cs
        //factory = config.BuildSessionFactory();
        factory = MyConfiguration.GetConfig().BuildSessionFactory();
        session = factory.OpenSession();
        #endregion