温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

NHibernate.cfg.xml文件总结

发布时间:2020-07-28 05:04:04 来源:网络 阅读:1141 作者:刺激乐天派 栏目:编程语言

   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路径中,以获得智能感知功能



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI