Nhibernate 三种配置方式

Nhibernate 三种配置方式

Posted on 2010-06-15 11:47 linFen 阅读(153) 评论(0) 编辑 收藏

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值