• NHibernate 基础


    install-package nhibernate

    install-package nunit

      Customer.cs

    public class Customer { public virtual Guid ID { get; set; } public virtual string Name { get; set; } public virtual string City { get; set; } }
    Customer.hbm.xml,需要设置为嵌入式资源,并与相应CS文件同DLL

    <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHi.Domain" namespace="NHi.Domain.Entities"> <class name="Customer" table="Customer"> <id name="ID" column="ID" type="Guid" > <generator class="assigned" /> </id> <property name="Name" type="string"> <column name="Name" sql-type="varchar(20)" not-null="false" /> </property> <property name="City" type="string"> <column name="City" sql-type="nvarchar(500)" not-null="false" /> </property> </class> </hibernate-mapping>
    hibernate.cfg.xml

    <?xml version="1.0" encoding="utf-8"?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHi.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">server=local;database=NHiDemo;uid=sa;pwd=123456;</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="hbm2ddl.auto">update</property> <!--貌似只能添加列,对删除列,修改类型/长度不起作用,待续。。。--> <mapping assembly="NHi.Domain"/> </session-factory> </hibernate-configuration>
    BaseTest.cs


    public class BaseTest { protected ISessionFactory sessionFactory; [SetUp] public void Init() { var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml"); sessionFactory = cfg.BuildSessionFactory(); }
                
               [TearDown]
               public void Teardown()
               {
              
               }
    }
    CustomerTest.cs
    [TestFixture]
    public class CustomerTest:BaseTest
    {
      
            [Test]
            public void Add()
            {
                    object o = null;
     
                    using (ISession session = sessionFactory.OpenSession())
                    {
                            var customer = new NHi.Domain.Entities.Customer()
                            {
                                    ID = Guid.NewGuid(),
                                    Name = "your name",
                                    City = "my city"
                            };
     
                            o=session.Save(customer);
                            session.Flush();
                    }
     
                    Assert.NotNull(o);
            }
    }
    
    

    NuGet 包:

    扩展和更新:

  • 相关阅读:
    Cocos2dx引擎10-事件派发
    IE无法打开internet网站已终止操作的解决的方法
    让程序在崩溃时体面的退出之Dump文件
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)
    cocos 3.0 一键打包android平台应该注意的细节
    Matlab画图-非常具体,非常全面
    linux和windows文件名称长度限制
    Javascript的DOM操作
    50个高端大气上档次的管理后台界面模板
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/4160130.html
Copyright © 2020-2023  润新知