想要实体与数据库关联起来就需要告诉nhibernate 我的数据库所在的位置,一般有两种方式 在Web.config 里面进行配置,还有一个写一个专门的xml文件进行配置,我所采用的是第一种
web.config 配置
<?
xml version="1.0"
?>
< configuration >
<!-- 使用configSections 声明一个名叫 hibernate-configuration 的节点 -->
< configSections >
< section name ="hibernate-configuration" type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</ configSections >
<!-- 描述节点 -->
< hibernate-configuration xmlns ="urn:nhibernate-configuration-2.2" >
< session-factory name ="WebApp" >
< property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >
Data Source=LLR\MSSQLSERVER2008;Initial Catalog=NHibernateSample;User ID=sa;Password=123
</ property >
< property name ="adonet.batch_size" > 10 </ property >
< property name ="show_sql" > true </ property >
< property name ="dialect" > NHibernate.Dialect.MsSql2008Dialect </ property >
< property name ="use_outer_join" > true </ property >
< property name ="command_timeout" > 10 </ property >
< property name ="query.substitutions" > true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle </ property >
<!-- 注意 这个小小的地方 是指定实体类所在的地方 -->
< mapping assembly ="llr.Models" />
</ session-factory >
</ hibernate-configuration >
< system.web >
< compilation debug ="false" targetFramework ="4.0" />
</ system.web >
</ configuration >
< configuration >
<!-- 使用configSections 声明一个名叫 hibernate-configuration 的节点 -->
< configSections >
< section name ="hibernate-configuration" type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</ configSections >
<!-- 描述节点 -->
< hibernate-configuration xmlns ="urn:nhibernate-configuration-2.2" >
< session-factory name ="WebApp" >
< property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >
Data Source=LLR\MSSQLSERVER2008;Initial Catalog=NHibernateSample;User ID=sa;Password=123
</ property >
< property name ="adonet.batch_size" > 10 </ property >
< property name ="show_sql" > true </ property >
< property name ="dialect" > NHibernate.Dialect.MsSql2008Dialect </ property >
< property name ="use_outer_join" > true </ property >
< property name ="command_timeout" > 10 </ property >
< property name ="query.substitutions" > true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle </ property >
<!-- 注意 这个小小的地方 是指定实体类所在的地方 -->
< mapping assembly ="llr.Models" />
</ session-factory >
</ hibernate-configuration >
< system.web >
< compilation debug ="false" targetFramework ="4.0" />
</ system.web >
</ configuration >
其它属性参考pdf 资料