配置NHibernate有三种常见的配置方法。
1:在web.config,App.config里面配置























则需要这样实例化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的格式








3:指明配置文件
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路径中,以获得智能感知功能。














一般会用以下的三种方法
<1> config.AddXmlFile("User.hbm.xml");
<2> config.AddClass(typeof(Test.User));
<3> config.AddAssembly("Test");




为了方便,把映射类及XML放到同一个程序集里面。然后使用config.AddAssembly(assemblyName)的方式,让NHibernate自动去查找指定程序集中所有的映射文件和映射类。
在配置文件里面加入
<mapping assembly="Test" />,
如:









那就省去了在代码里面捆绑程序集了。