NHibernate.cfg.xml文件总结
A. 问题:
NHibernate.cfg.xml用来干什么的?
答:
配置数据库的基本信息和configuration和sessionFactory实例的基本信息的配置文件。
B. 如何配置NHibernate.cfg.xml文件?
答:
第一种方式
Configuration config = new Configuration();
这种配置方法将会到应用程序配置文件(App.Config,Web.Config)中查找NHibernate的配置信息,NHibernate的配置节必须符合应用程序配置文件个格式。
下面是该种方式的一个例子:
<?xmlversion="1.0"encoding="utf-8"?><configuration><!--Addthiselement--><configSections><sectionname="hibernate-configuration"type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/></configSections><!--Addthiselement--><hibernate-configurationxmlns="urn:nhibernate-configuration-2.2"><session-factory><propertyname="dialect">NHibernate.Dialect.MsSql2005Dialect</property><propertyname="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><propertyname="connection.connection_string">Server=TLSZ207/SQLEXPRESS;initialcatalog=Test;IntegratedSecurity=true</property></session-factory></hibernate-configuration><!--Leavethesystem.websectionunchanged--><system.web></system.web></configuration>
2.第二种方式
Configuration config = new Configuration().Configure();
这种配置方法将会在应用的相同目录查找名为”hibernate.cfg.xml”的标准Hibernate配置
<?xmlversion="1.0"encoding="utf-8"?><hibernate-configurationxmlns="urn:nhibernate-configuration-2.0"><session-factoryname="MySessionFactory"><!--properties--><propertyname="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><propertyname="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><propertyname="connection.connection_string">Server=localhost;initialcatalog=Hibernate;IntegratedSecurity=SSPI</property><propertyname="show_sql">false</property><propertyname="dialect">NHibernate.Dialect.MsSql2000Dialect</property><propertyname="use_outer_join">true</property><propertyname="query.substitutions">true1,false0,yes'Y',no'N'</property><!--mappingfiles--><mappingassembly="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路径中,以获得智能感知功能
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。