NH4—NHibernate的配置文件方式

三、Nhibernate的配置文件

其实nh的官网提供的配置方式有不少种,我在这总结咱们最最经常使用的一些方法。html

http://www.cnblogs.com/gooddasenlin/archive/2008/08/21/1273581.html  参考这篇文章spring

多种配置方式混合使用
sql

(1)写在config文件中,这样会致使config文件太大,很差管理session

ToDo:配置文件的代码例子app

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration"
         type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">Server=.;initial catalog=Db0515Demo;Integrated Security=SSPI</property>
      <mapping assembly="NHDemo" />
    </session-factory>
  </hibernate-configuration>  
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>




(2)引用外部文件的写法ide

1)应用外部文件通常能够从程序获取外部文件的时候选择路径,但我这里只说配置spa

2)他有一个默认的就是在config相同的目录下面的找名称为hibernate.cfg.xml名称的文件,还有要把这个文件的属性设置为老是复制。
.net

<?xml version="1.0" encoding="utf-8" ?>
<!--SQL配置方式-->
<!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.connection_string">Server=(local);initial catalog=MyNHibernate;Integrated Security=SSPI</property>
    <property name="connection.isolation">ReadCommitted</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>-->

<!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="connection.connection_string">Server=(local);initial catalog=MyNHibernate;Integrated Security=SSPI</property>
      <property name="connection.isolation">ReadCommitted</property>
      <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
      </property>
    </session-factory>
  </hibernate-configuration>-->

<!--Oracle配置方式-->
<!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property>
    <property name="connection.isolation">ReadCommitted</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>-->

<!--Oracle配置方式(最新版本Nhibernate3以上)这些能够直接去官网复制-->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="MyManager">
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property>
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  </session-factory>
</hibernate-configuration>

<!--<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    -->
<!--<property name="connection.connection_string">Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=11.101.9.54)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));user id=C##MSWGR;password=mswgr;</property>-->
<!--
    -->
<!--        <property name="default_schema">AdventureWorksLT.SalesLT</property>-->
<!--
    <property name="connection.connection_string">User ID=apt;Password=pwd;Data Source=orcl</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    -->
<!--    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>-->
<!--
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="show_sql">true</property>
    <property name="connection.release_mode">auto</property>
    <property name="adonet.batch_size">500</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    -->
<!-- hbm2ddl tool property should NOT be used in production and is here to get you 
        going with the Cookbook! -->
<!--
    <property name="hbm2ddl.auto">update</property>
    <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
    -->
<!-- Mapping assemblies -->
<!--
    -->
<!-- Can't map it for Fluent NHibernate here; instead, load the mapping assembly in Global.asax.cs.
            If you're still using HBMs, you can use the mapping here or pass the assembly via Global.asax.cs
            as well, just like you can do with the Fluent NHibernate assembly(s). -->
<!--
  </session-factory>


</hibernate-configuration>-->

3)ToDo:各个参数内容的意思
hibernate


4)外部文件引用位置的一个问题code

个人配置文件是放在UI层项目中的,而后在test程序集中没有引用,因此报的错误,解决办法就是在test程序集中添加对UI层的引用,问题截图。


(3)用fluent这种代码配置的方式

(4)集成在spring.net这种IoC容器中的配置(主要是一些写法的不一样)


Loquacious配置

相关文章
相关标签/搜索