NHibernate入门学习

我使用的是NHibernate4.0的版本。

操作步骤

1.你当然要下载NHibernate的DLL引用到项目中去;

2.配置NHibernate.cfg.xml這个文件啦,這个文件一定要的,我這里在目录下创建了一个新的XML格式文件;

   也可以将NHibernate连接数据库相关的配置到App.config/Web.config中;

<?xml version="1.0" encoding="utf-8"?>
<!-- 
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="ConsoleApplication1">
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <!-- 链接数据库字符串 -->
    <property name="connection.connection_string">Server=(local);Database=Test_base;Uid=sa;Pwd=123</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="adonet.batch_size">10</property>
    <property name="show_sql">true</property>
    <property name="command_timeout">60</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <!-- 這段非常重要,這是去寻找你的实体类的程序集 -->
    <mapping assembly="Domion.Entity" />
  </session-factory>
</hibernate-configuration>
3.新建实体模型对象和XX.hbm.xml配置文件,配置文件和实体对象类必须在同目录格式下.

实体对象(一定要是"virtual"):

    [Serializable]
    public class Test
    {
        public virtual string Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
        public virtual string Descs { get; set; }
    }
Nhibernate映射文件是以.hbm.xml格式结尾,文件名与类名相同:如Test.hbm.xml

<?xml version="1.0" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="实体对象程序集:如Domion.Entity" namespace="实体类对象命名空间如:Domion.Entity">
  <class name="Test" table="Test">
    <id name="Id"  unsaved-value="0" type="string">
      <column name="Id" length="50" sql-type="nvarchar" not-null="true" unique="true" index="PK_Test" />
      <!--<generator class="assigned " />-->
      <!-- unsaved-value used to be null and generator was increment in h2.0.3 -->
    </id>

    <property name="Name" type="string" >
      <column name="Name"  length="50" not-null="false" sql-type="nvarchar"  />
    </property>

    <property name="Age" type="int" >
      <column name="Age"  length="50" not-null="false" sql-type="int" />
    </property>
    <property name="Descs" type="string" >
      <column name="Descs"  length="50" not-null="false" sql-type="nvarchar" />
    </property>
  </class>
</hibernate-mapping>
3.所有工作完成后,就开始写测试运行程序了:

 

        Test test = new Test
            {
                Id = "ddd",
                Age = 12,
                Name = "马云屌丝",
                Descs = "test"
            };           

            var cfg = new NHibernate.Cfg.Configuration();
            cfg.Configure(AppDomain.CurrentDomain.BaseDirectory + "Required_Bins\\NHibernate.cfg.xml");
            ISessionFactory sessionFactory = cfg.BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            try
            {
                var obj = session.Save(test);
                session.Flush();
                //session.Save(test);
                //session.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值