NHibernate配置及映射文件
2009-04-02 23:39
351 查看
配置NHibernate有三种常见的配置方法。
1:在web.config,App.config里面配置
<?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>
则需要这样实例化Configuration对象。
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
这种配置方法将会到应用程序配置文件(App.Config,Web.Config)中查找NHibernate的配置信息.
2:hibernate.cfg.xml
建立名为hibernate.cfg.xml的文件。实例化Configuration config = new Configuration().Configure();这样NHibernate将会在目录下寻找hibernate.cfg.xml的配置文件。
hibernate.cfg.xml的格式
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory name="MySessionFactory">
<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>
指明配置文件
Configuration config = new Configuration().Configure(configFileName);
这种配置方法将查找指定的Hibernate标准配置文件,可以是绝对路径或者相对路径。还可以通过编码的方式来添加配置信息:
IDictionary props = new Hashtable();
props[“dialect”] = "NHibernate.Dialect.MsSql2005Dialect";
...
Configuration cfg = new Configuration();
cfg.Properties = props;//cfg.AddProperties(props);
映射文件:
所有的XML映射都需要使用nhibernate-mapping-2.0 schema。目前的schema可以在NHibernate的资源路径或者是NHibernate.dll的嵌入资源(Embedded Resource)中找到。NHibernate总是会优先使用嵌入在资源中的schema文件。你可以将hibernate-mapping拷贝到C:/Program Files/Microsoft Visual Studio .NET 2003/Common7/Packages/schemas/xml路径中,以获得智能感知功能。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test" assembly="Test">
<class name="Test.Cat,Test" table="Cat">
<id name="CatID">
<column name="CatID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight" />
</class>
</hibernate-mapping>
1:在web.config,App.config里面配置
<?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>
则需要这样实例化Configuration对象。
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
这种配置方法将会到应用程序配置文件(App.Config,Web.Config)中查找NHibernate的配置信息.
2:hibernate.cfg.xml
建立名为hibernate.cfg.xml的文件。实例化Configuration config = new Configuration().Configure();这样NHibernate将会在目录下寻找hibernate.cfg.xml的配置文件。
hibernate.cfg.xml的格式
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory name="MySessionFactory">
<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>
指明配置文件
Configuration config = new Configuration().Configure(configFileName);
这种配置方法将查找指定的Hibernate标准配置文件,可以是绝对路径或者相对路径。还可以通过编码的方式来添加配置信息:
IDictionary props = new Hashtable();
props[“dialect”] = "NHibernate.Dialect.MsSql2005Dialect";
...
Configuration cfg = new Configuration();
cfg.Properties = props;//cfg.AddProperties(props);
映射文件:
所有的XML映射都需要使用nhibernate-mapping-2.0 schema。目前的schema可以在NHibernate的资源路径或者是NHibernate.dll的嵌入资源(Embedded Resource)中找到。NHibernate总是会优先使用嵌入在资源中的schema文件。你可以将hibernate-mapping拷贝到C:/Program Files/Microsoft Visual Studio .NET 2003/Common7/Packages/schemas/xml路径中,以获得智能感知功能。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test" assembly="Test">
<class name="Test.Cat,Test" table="Cat">
<id name="CatID">
<column name="CatID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight" />
</class>
</hibernate-mapping>
相关文章推荐
- NHibernate配置和映射文件的智能提示
- NHibernate配置和映射文件的智能提示
- [转]NHibernate配置及映射文件
- NHibernate 映射文件中主键的不同配置方法
- NHibernate配置及映射文件
- NHibernate配置及映射文件
- NHibernate之映射文件配置说明(转载1)
- NHibernate之映射文件配置说明
- NHibernate配置和映射文件的IntelliSense智能提示
- NHibernate之映射文件配置说明(转载3)
- nhibernate配置和持久对象映射文件
- NHibernate配置和映射文件的智能提示
- NHibernate配置和映射文件的智能提示
- NHibernate之映射文件配置说明
- NHibernate 非自增主键与自增主键的映射文件配置
- NHibernate配置文件和映射文件的详解
- NHIBERNATE之映射文件配置说明(转载4)
- NHibernate配置及映射文件
- 如果Oracle中的字段和数据库的系统字段重名,怎么配置NHibernate的映射实体文件
- NHibernate配置文件和映射文件的详解