返回首页

高速

返回单一值 类似Scalar

session.CreateSQLQuery(sql).UniqueResult<int>()

高速

返回单一实体

session.CreateSQLQuery(sql).AddEntity(typeof(MyClass)).UniqueResult<MyClass>()

使用CreateSQLQuery时 如果直接返回集合.List<Entity> 会报System.Object[] 不是""类型,不能再此集合中使用
可以 CreateSQLQuery().AddEntity(typeof(Entity)) 或 SetResultTransformer(Transformers.AliasToBean(typeof(Entity)))

提示 The entity type: was not register in NHibernate.Mapping nhibernate

VS中使用时,对配置文件 hbm.xml 结尾的 需右键属性 设置为嵌入的资源 还有就是该文件必须是hbm.xml格式的文件 不能只是.xml

知识点

Nhibernate Profiler 的使用

首先下载该工具工具链接
解压后,内容如下:
在项目中引用HibernatingRhinos.Profiler.Appender的dll
在代码中增加初始化:
var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml"); HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); --这句 sessionFactory = cfg.BuildSessionFactory();
打开NHibernate Profiler,并且运行你的项目就可以在NHibernate Profiler里看到SQL语句了