欢迎光临成都祈钰瑶科技有限公司网站!祝您生活愉快!
热线电话: 18982081108
✕

成都服务器托管,成都GPU服务器租用,成都高防BGP宽带租用,成都机柜租用托管

NHibernate.cfg.xml文件总结

   A. 问题:

成都创新互联公司专注于贵溪网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供贵溪营销型网站建设,贵溪网站制作、贵溪网页设计、贵溪网站官网定制、小程序开发服务,打造贵溪网络公司原创品牌,更为您提供贵溪网站排名全网营销落地服务。

    NHibernate.cfg.xml用来干什么的?

    答:

        配置数据库的基本信息和configuration和sessionFactory实例的基本信息的配置文件。

    

   B. 如何配置NHibernate.cfg.xml文件?

    答:

  1. 第一种方式

    Configuration config = new Configuration();

    这种配置方法将会到应用程序配置文件(App.Config,Web.Config)中查找NHibernate的配置信息,NHibernate的配置节必须符合应用程序配置文件个格式。

    下面是该种方式的一个例子:

    

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Add this element -->
<configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>

<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=TLSZ207/SQLEXPRESS;initial catalog=Test;Integrated Security=true</property>
</session-factory>
</hibernate-configuration>
<!-- Leave the system.web section unchanged -->
<system.web>
</system.web>
</configuration>

2.第二种方式

    Configuration config = new Configuration().Configure();

这种配置方法将会在应用的相同目录查找名为”hibernate.cfg.xml”的标准Hibernate配置

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.0" >
 <session-factory name="MySessionFactory">
              <!-- properties -->
              <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
              <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
              <property name="connection.connection_string">Server=localhost;initial catalog=Hibernate;Integrated Security=SSPI</property>
              <property name="show_sql">false</property>
              <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
              <property name="use_outer_join">true</property>
              <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

              <!-- mapping files -->
              <mapping assembly="Test.Model" />
       </session-factory>
</hibernate-configuration>

3.第三方式

这种配置方法将查找指定的Hibernate标准配置文件,可以是绝对路径或者相对路径。还可以通过编码的方式来添加配置信息:Configuration config = new Configuration().Configure(NHibernate.cfg.xml配置文件路径);

映射文件: 
  所有的XML映射都需要使用nhibernate-mapping-2.0 schema。目前的schema可以在NHibernate的资源路径或者是NHibernate.dll的嵌入资源(Embedded Resource)中找到。NHibernate总是会优先使用嵌入在资源中的schema文件。你可以将hibernate-mapping拷贝到C: /Program Files/Microsoft Visual Studio .NET 2013/Common7/Packages/schemas/xml路径中,以获得智能感知功能