NHibernate使用笔记

      最近看了好多ORM库,自己也尝试过自己写一些代码,终于还是决定使用 NHibernate。至于具体的使用,过去不是很了解,所以也是从头开始。
      从sf下载bin和source 包,把它们放到对应的位置,开始一个新的探索过程了。对于网上的很多文章和附带的文档,感觉很难把握要领。所以先从nhibernate的附带的example开始,并学习在web项目中使用它。一边学一边用,也许是个不错的方法。

      创建一个空的web项目,第一目录是要在这个项目中使用NHibernate,配置库,添加引用是开始。添加引用比较简单,直接复制文件或者通过ide添加引用到bin目录后,就基本完成。接下来要配置web.config了。看了帮助用的QuickStart,添加如下内容,基本上也就算完成了。

  <configSections>
    
<section name="hibernate" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  
</configSections>

  
<hibernate xmlns="urn:nhibernate-configuration-2.2">
    
<session-factory>
      
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
      
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      
<property name="connection.connection_string">Server=(local);Initial Catalog=xxxx;Persist Security Info=True;User ID=xxxx;Password=xxxx</property>

      
<mapping assembly="QuickStart" />
    
</session-factory>
  
</hibernate>
    
   配置完后,开始了解NHibernate是在那些位置被使用的。

   debug example的项目,在vs2005里看看执行的过程。 程序首先执行到了static ExampleApplication()
      log4net.Config.XmlConfigurator.Configure();
   Configuration 
= new NHibernate.Cfg.Configuration()
    .SetDefaultAssembly(
typeof(Item).Assembly.FullName)
    .SetDefaultNamespace(
typeof(Item).Namespace)
    .AddDirectory(
new DirectoryInfo(HostingEnvironment.MapPath("~/App_Data/")));
   
   SessionFactory 
= Configuration.BuildSessionFactory();
    这个部分是执行程序初始化配置的,具体的用法,现在需要看一下附带的help文件了(Chapter 3. ISessionFactory Configuration--- 3.1. Programmatic Configuration)。  http://www.cnblogs.com/abluedog/archive/2006/04/17/377630.html  这个系列里有不少的介绍。现在正在了解 “在取得config后,我们还需要进行映射文件添加”这个部分。但是新版的添加方式已经增加了,可以看看NHibernate.Cfg.Configuration.Addxxxx类的方法。
原文地址:https://www.cnblogs.com/thh/p/673703.html