更新:现在已解决,请参阅下面的答案。正确的NHibernate映射存储过程?
我有一点试图找出编写存储过程MSSQL的NHibernate的映射文件(的.hbm.xml)的正确方法的麻烦。
存储过程接受两个参数并返回包含多个具有整数值的列的单个行结果集。
我的映射文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TestApp.DataAccess"
namespace="TestApp.DataAccess.Models">
<class name="MonthlyInstructionCount" lazy="true">
<id name="Id" column="Month">
<generator class="native" />
</id>
<property name="Month" />
<property name="MapRequests" />
<property name="Instructions" />
<property name="DrainsLookSee" />
<property name="DrainsFullCctv" />
<property name="Soils" />
<property name="Roots"/>
<property name="Arb" />
<loader query-ref="MI_MonthlyInstructionCount"/>
</class>
<sql-query name="MI_MonthlyInstructionCount">
<return class ="MonthlyInstructionCount">
<return-property name="Month" column="Month" />
<return-property name="MapRequests" column="MapRequests" />
<return-property name="Instructions" column="Instructions" />
<return-property name="DrainsLookSee" column="DrainsLookSee" />
<return-property name="DrainsFullCctv" column="DrainsFullCctv" />
<return-property name="Soils" column="Soils" />
<return-property name="Roots" column="Roots" />
<return-property name="Arb" column="Arb" />
</return>
exec dbo.MI_MonthlyInstructionCount :StartDate :NumberOfMonths
</sql-query>
</hibernate-mapping>
我也曾尝试以下,有人在为另一个人是具有与存储过程类似问题论坛的建议......
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TestApp.DataAccess"
namespace="TestApp.DataAccess.Models">
<sql-query name="MI_MonthlyInstructionCount">
<return class ="MonthlyInstructionCount" />
exec dbo.MI_MonthlyInstructionCount :StartDate :NumberOfMonths
</sql-query>
</hibernate-mapping>
两者都不似乎工作...我只是得到以下错误:
The type initializer for 'TestApp.DataAccess.Sql.NHibernateHelper' threw an exception.
{"Errors in named queries: {MI_MonthlyInstructionCount}"}
我看不到它是存储过程(虽然错误会显示它是...),因为它似乎在MSSQL Server Manager中成功运行。
任何帮助非常感谢!干杯!
MonthInstructionCount需要是映射类吗?从你的例子中不清楚,但我会认为这将是一个DTO,并可能更好地使用投影处理。 – DanP 2010-08-18 15:04:48
最终,我需要在6个月的时间内显示“指令计数”表。我们已经编写了一个可以显示对象列表的类似DataTable的组件,所以我们希望使用它来显示这些信息。我们的DataTable组件需要IEnumerable形式的数据源。因此需要映射类的原因。我们需要能够传递一个IEnumerable 作为它的数据源。谢谢 –
2010-08-19 08:32:50