首页 > 代码库 > NHibernate 基础
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
<?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(); } }
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); }}
NHibernate 基础
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。