NHibernate 入门

  • NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。
  • 在今日的企业环境中,把面向对象软件和关系数据库一起使用可能是相当麻烦和浪费时间的.NHibernate
      

    NHibernate

    不仅仅管理.NET类到数据库表的映射(包括.NET 数据类型到SQL数据类型的映射),还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和ADO.NET处理数据的时间。
  • Hibernate是一个目前应用的最广泛的开放源代码的对象关系映射框架,它对Java的JDBC(类似于ADO.Net)进行了非常轻量级的对象封装
  • NHibernate 从数据库底层来持久化你的.Net 对象到关系型数据库。NHibernate 为你处理这些,远胜于你不得不写SQL去从数据库存取对象。你的代码仅仅和对象关联,NHibernat 自动产生SQL语句,并确保对象提交到正确的表和字段中去。

关于NHibernate 的配置问题:
第一种在代码里写:
var cfg = new Configuration();
 
cfg.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
cfg.SetProperty("dialect", "NHibernate.Dialect.SQLiteDialect");
cfg.SetProperty("connection.connection_string", "Data Source=:memory:;Version=3;New=True;");
cfg.SetProperty("connection.release_mode", "on_close");
cfg.SetProperty("show_sql", "true");
 
cfg.AddAssembly(typeof (Blog).Assembly);
 
ISessionFactory factory = cfg.BuildSessionFactory();

第二种在web.config文件里写:(注意命名,一定要一样的)
<configSections>

    <section name="hibernate-configuration"         
             type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=(local);database=thedatabase;Integrated Security=SSPI;</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>

猜你喜欢

转载自evencode.iteye.com/blog/1145376