NH第一个nhibernate程序
第一步:下载程序对应的程序集
https://sourceforge.net/projects/nhibernate/files/NHibernate/

下载程序集后看到项目有如下DLL与对应的配置文件


第二步:在对应程序中添加对应的配置文件(主要作用通过配置的形式初始货session-factory对象)
配置如下:
<?xml version="1.0" encoding="utf-8"?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <!--数据库连接字符串--> <property name="connection.connection_string"> server=(local);database=WISEDB;uid=sa;pwd=123; </property> <!--设置NHibernate的Dialect类名 - 允许NHibernate针对特定的关系数据库生成优化的SQL--> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <!--映射文件所在程序集空间--> <mapping assembly="Common"/> </session-factory> </hibernate-configuration>
第三步:添加对应的映射文件‘(XML生成操作设置为必须嵌入的资源)
using System; using System.Text; using System.Collections.Generic; namespace Common { public class GoodsType { public virtual int Id { get; set; } public virtual string GoodsTypeName { get; set; } public virtual string Remark { get; set; } public virtual int? Sort { get; set; } public virtual int? State { get; set; } public virtual DateTime? CreateTime { get; set; } public virtual int? CreateUserId { get; set; } public virtual int? ModifyUserId { get; set; } public virtual DateTime? ModifyTime { get; set; } } }
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly="Common" namespace="Common" xmlns="urn:nhibernate-mapping-2.2"> <class name="GoodsType" table="goods_type" lazy="true" > <id name="Id" column="id"> <generator class="identity" /> </id> <property name="GoodsTypeName"> <column name="goods_type_name" sql-type="varchar" not-null="true" /> </property> <property name="Remark"> <column name="remark" sql-type="varchar" not-null="false" /> </property> <property name="Sort"> <column name="sort" sql-type="int" not-null="false" /> </property> <property name="State"> <column name="state" sql-type="int" not-null="false" /> </property> <property name="CreateTime"> <column name="create_time" sql-type="datetime" not-null="false" /> </property> <property name="CreateUserId"> <column name="create_user_id" sql-type="int" not-null="false" /> </property> <property name="ModifyUserId"> <column name="modify_user_id" sql-type="int" not-null="false" /> </property> <property name="ModifyTime"> <column name="modify_time" sql-type="datetime" not-null="false" /> </property> </class> </hibernate-mapping>

第四:测试
public static List<GoodsType> Text() { List<GoodsType> goods = null; //获取hibernate配置文件 实现ISessionFactory工厂初始参数 var cfg = new NHibernate.Cfg.Configuration().Configure("./../../Config/hibernate.cfg.xml"); //生成ISessionFactory工厂 ISessionFactory sessionFactory = cfg.BuildSessionFactory(); //ISessionFactory工厂生成ISession ISession直接与操作数据库 using (ISession session = sessionFactory.OpenSession()) { goods= session.Query<GoodsType>().ToList(); } return goods; }
链接:http://pan.baidu.com/s/1i5GLDxb 密码:jj3r
遇到问题:
原因:配置文件没有添加对应映射程序集空间

解决方法:

问题二:原因:添加NH.DLL与引用的framework版本不一致(解决方法:安装对应的framework片本)

浙公网安备 33010602011771号