存储过程在nHibernate中不起作用 [英] Stored Procedure don't work in nHibernate
问题描述
目标:
在NHibernate的支持下,将数据检索到一个类中.您可以使用NHibernate通过存储过程来检索数据.
Goal:
retrieve data into a class with support of a stored procedure with NHibernate. You use NHibernate to retrieve the data by using stored procedure.
问题:
我检索到一条错误消息:
Problem:
I retrieve a error message saying:
类型的未处理异常 NHibernate.dll中发生了"NHibernate.Exceptions.GenericADOException"
An unhandled exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll
其他信息:无法执行查询
Additional information: could not execute query
[exec sp_retrieveAllProductCategory]
[ exec sp_retrieveAllProductCategory ]
[SQL:exec sp_retrieveAllProductCategory]
[SQL: exec sp_retrieveAllProductCategory]
在这种情况下我不知道该怎么办.
I dont know what to do in this context.
信息:
我正在使用VS 2013和SQL Server 2012
I'm using VS 2013 and SQL Server 2012
test.hbm.xml
test.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="data_layer" namespace="data_layer">
<sql-query name="sp_retrieveAllProductCategory">
<return class="Produkt">
<return-property column="Produkt_kategori" name="Produkt_kategori" />
<return-property column="Produkt_kategori_ordningsnummer" name="Produkt_kategori_ordningsnummer" />
</return>
exec sp_retrieveAllProductCategory
</sql-query>
</hibernate-mapping>
----------------------
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=SHAREPOINT01;Initial Catalog=Active_system;Integrated Security=True</property>
<property name="show_sql">false</property>
<mapping assembly="data_layer"/>
</session-factory>
</hibernate-configuration>
-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace data_layer
{
public class NhibernateDataProvider
{
public NhibernateDataProvider()
{
_sessionFactory = new Configuration().Configure().BuildSessionFactory();
_session = _sessionFactory.OpenSession();
}
private ISessionFactory _sessionFactory;
private ISession _session;
public IList<Produkt> GetAllEmployee()
{
return _session.CreateCriteria<Produkt>().List<Produkt>();
}
public void GetNamedQuery()
{
IQuery query = _session.GetNamedQuery("sp_retrieveAllProductCategory");
// IList<Product> products = query.List<Product>();
IList<Produkt> products = query.List<Produkt>();
}
}
}
-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data_layer
{
public class Produkt
{
public virtual string Produkt_kategori { get; set;}
public virtual int Produkt_kategori_ordningsnummer { get; set;}
}
}
推荐答案
IList<Produkt> sss = _session
.GetNamedQuery("sp_retrieveAllProductCategory")
.SetResultTransformer(
Transformers.AliasToBean(typeof(Produkt))).List<Produkt>();
https://www.simple-talk.com/blogs/2013/09/27/nhibernate-and-stored-procedures-in-c/
这篇关于存储过程在nHibernate中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!