NHibernate入门学习

来源:互联网 发布:php截取html字符串 编辑:程序博客网 时间:2024/05/20 04:28

我使用的是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 0
原创粉丝点击