码迷,mamicode.com
首页 > Web开发 > 详细

NHibernate 3.3

时间:2014-10-24 18:05:23      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   for   sp   文件   

今天试了一下NHibernate 3.3比之前的版本简单,只需要引入两个dll,这两个dll分别是:Iesi.Collections.dll和NHibernate.dll

通过http://nhforge.org/下载NHibernate的语言件

config文件配置如下

  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
  </configSections>
  
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="NHibernate.Test">
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">
        Server=(local);initial catalog=BitAuto;User ID=sa;Password=num777+
      </property>
      
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="show_sql">flase</property>      
      <property name="command_timeout">10</property>
      <property name="query.substitutions">true 1, false 0, yes ‘Y‘, no ‘N‘</property>
      <mapping assembly="Model"/>
    </session-factory>
  </hibernate-configuration>

实体mapping配置文件如下

<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
  <class name="Model.UserInfo" table="UserInfo" lazy="true">

    <id name="Id" column="Id" type="string">
      <generator class="assigned" />
    </id>
    <property name="Name" column="Name" type="string" />
    <property name="Sex" column="Sex" type="string" />
    <property name="Age" column="Age" type="short" />
  </class>
</hibernate-mapping>

C#代码如下

var config = new Configuration();
            config.Configure();
            using(var factory = config.BuildSessionFactory())
            {
                using (var session = factory.OpenSession())
                {
                    var trans = session.BeginTransaction();
                    try
                    {
                        var list = session.CreateQuery("from UserInfo where Name=?").SetParameter(0, "小红").List<UserInfo>();
                        foreach (var item in list)
                        {
                            Console.WriteLine("{0}-{1}", item.Id, item.Name);
                        }
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        Console.WriteLine(ex.Message);
                    }

                    Console.Read();
                }
            }

 

NHibernate 3.3

标签:style   blog   http   color   io   ar   for   sp   文件   

原文地址:http://www.cnblogs.com/bygrace/p/4048707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!