文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>NHibernate入门示例1

NHibernate入门示例1

时间:2010-09-20  来源:吴X

 

create table Cat2(
CatId char(32) not null primary key,
Name nvarchar(16) not null,
Sex nchar(1),
Weight [real]
)

 

 

代码 <configuration>
  
  <!-- Add this element -->
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>

  <!-- Add this element -->
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=192.168.10.99;initial catalog=DProjectWCGTest;uid=sa;pwd=Admin12345;</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      
      <property name="show_sql">true</property>
     
      <!--<mapping resource="TestStaticContructor.Models.Cat.hbm.xml" assembly="TestStaticContructor" />-->
      <mapping  assembly="TestStaticContructor" />
    </session-factory>
  </hibernate-configuration>
  
</configuration>

 

 

代码 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestStaticContructor.Models" assembly="TestStaticContructor">
  
  <!--
  独立的实体: e.g.
  <class name="Test.Model.Person, Test.Model" table="Person">
-->
  <class name="Cat" table="Cat">
    <!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
    <id name="Id">
      <column name="CatId" sql-type="char(32)" not-null="true"/>
      <generator class="uuid.hex" />
    </id>
    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="Name">
      <column name="Name" length="16" not-null="true" />
    </property>
    <property name="Sex" />
    <property name="Weight" />
  </class>
</hibernate-mapping>

 

 

代码 public class Cat
    {
        private string id;
        private string name;
        private char sex;
        private float weight;
        public Cat()
        {
        }
        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }
        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }
        public virtual char Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        public virtual float Weight
        {
            get { return weight; }
            set { weight = value; }
        }
    }

 

代码   static void Main(string[] args)
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

            ISession session = sessionFactory.OpenSession();

            Cat2 cat = new Cat2();
            cat.Name = "Cat2";

            Console.WriteLine("cat.name={0}",cat.Name.Length);

            cat.Sex = '女';
            cat.Weight = 3.14f;
            ITransaction tran = session.BeginTransaction();
            try
            {
                session.SaveOrUpdate(cat);
                tran.Commit();
            }
            catch (Exception )
            {
                tran.Rollback();
                throw;
            }
            session.Close();
            sessionFactory.Close();

            Console.WriteLine("done");
            ////
            //MyClass.SayHi();
            //MyClass mc = new MyClass();
            Console.ReadKey();
        }

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载