天天看點

Nhibernate3循序漸進(一) - 第一個NHibernate程式

1. 建立Domain項目

  Product.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace NHibernate3.Domain

{

    public class Product

    {

        /// <summary>

        /// ID

        /// </summary>

        public virtual Guid ID { get; set; }

        /// 編号

        public virtual string Code { get; set; }

        /// 名稱

        public virtual string Name { get; set; }

        /// 規格

        public virtual string QuantityPerUnit { get; set; }

        /// 機關

        public virtual string Unit { get; set; }

        /// 售價

        public virtual decimal SellPrice { get; set; }

        /// 進價

        public virtual decimal BuyPrice { get; set; }

        /// 備注

        public virtual string Remark { get; set; }

    }

}

編寫映射檔案Product.hbm.xml

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

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate3.Domain" namespace="NHibernate3.Domain">

  <class name="Product" table="T_Product" lazy="true" >

    <id name="ID" column="ID" type="Guid" >

      <generator class="assigned" />

    </id>

    <property name="Code" type="string">

      <column name="Code" length="50"/>

    </property>

    <property name="Name" type="string">

      <column name="Name" length="50"/>

    <property name="QuantityPerUnit" type="string">

      <column name="QuantityPerUnit" length="50"/>

    <property name="Unit" type="string">

      <column name="Unit" length="50"/>

    <property name="SellPrice" type="decimal">

      <column name="SellPrice" precision="14" scale="2"/>

    <property name="BuyPrice" type="decimal">

      <column name="BuyPrice" precision="14" scale="2"/>

    <property name="Remark" type="string">

      <column name="Remark" length="200"/>

  </class>

</hibernate-mapping>

然後,将映射檔案“Product.hbm.xml”的屬性“生成方式”設定為“嵌入的資源”

Nhibernate3循序漸進(一) - 第一個NHibernate程式

2. 建立測試項目

  引用程式集“Antlr3.Runtime.dll”,“Iesi.Collections.dll”,“NHibernate.dll”,“Remotion.Data.Linq.dll”,  “nunit.framework.dll”, 添加對Domain項目的引用

  建立Config目錄,複制配置檔案模闆

hibernate.cfg.xml

<?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="NHibernate3.Test">

        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>

        <property name="connection.connection_string">

      server=.;database=NHibernateDemo;integrated security=SSPI;

        <property name="adonet.batch_size">10</property>

        <property name="show_sql">true</property>

        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>

        <property name="use_outer_join">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>

        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

    <mapping assembly="NHibernate3.Domain"/>

    </session-factory>

</hibernate-configuration>

修改該檔案的屬性為“始終複制”

Nhibernate3循序漸進(一) - 第一個NHibernate程式

複制proxyfactory類的程式集“LinFu.DynamicProxy.dll”和“NHibernate.ByteCode.LinFu.dll”到項目中,并修改生成方式

建立“NHibernateInit.cs”類檔案,用于初始化資料庫的表結構

NHibernateInit.cs

using NHibernate;

using NHibernate.Cfg;

//using NUnit.Framework;

namespace NHibernate3.Test

    public class NHibernateInit

        public static void InitTest()

        {

            var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");

            using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { }

        }

3. 建立資料庫結構

  在SQL Server2008中, 建立名為“NHibernateDemo”的資料庫

  在控制台的Main方法中, 加入如下代碼:

NHibernateInit.InitTest();

Console.WriteLine("資料庫建立成功!");

Console.Read();

4. 運作程式

  運作後我們發現NHibernate為我們自動建立了T_Product資料表