A. 问题:
NHibernate.cfg.xml用来干什么的?
答:
配置数据库的基本信息和configuration和sessionFactory实例的基本信息的配置文件。
B. 如何配置NHibernate.cfg.xml文件?
答:
第一种方式
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.第三方式
映射文件:
所有的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路径中,以获得智能感知功能
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。