配置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 >
<? 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 >
<? 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 >