命名查询中的NHibernate错误

时间:2017-07-26 12:50:52

标签: c# nhibernate nhibernate-mapping

我有一个班级:

public class OrderDetails
{
    public virtual int OrderNumber { get; set; }
    public virtual DateTime OrderDate { get; set; }
    public virtual int ProductCode { get; set; }
    public virtual string ProductName { get; set; }
    public virtual int Quantity { get; set; }
    public virtual decimal Price { get; set; }
}

为它映射.hbm.xml文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="AsmName" namespace="AsmName.Models">
  <sql-query name="GetOrdersByDate" callable="true">
    <return class="AsmName.Models.OrderDetails">
      <return-property name="OrderNumber" column="ID"/>
      <return-property name="OrderDate" column="OrderDate"/>
      <return-property name="ProductCode" column="productCode"/>
      <return-property name="ProductName" column="Name"/>
      <return-property name="Quantity" column="Quantity"/>
      <return-property name="Price" column="UnitPrice"/>
    </return>
    exec [dbo].[sp_OrdersByDate] :dateFrom, :dateTo
  </sql-query>
</hibernate-mapping>

所以,我试图从db调用存储过程并将其映射到类。在configuration.BuildSessionFactory();我在命名查询中遇到异常&#34;错误:{GetOrdersByDate}&#34;。内部异常为空。 堆栈跟踪是:

 NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   в NHibernate.Cfg.Configuration.BuildSessionFactory()
   в Fuse8Test.Models.DAL.NHibirnateSession.OpenSession() в c:\users\itenberg\documents\visual studio 2017\Projects\Fuse8Test\Fuse8Test\Models\DAL\NHibirnateSession.cs:строка 19
   в Fuse8Test.Models.DAL.DBContext.GetReport(DateTime dateFrom, DateTime dateTo) в c:\users\itenberg\documents\visual studio 2017\Projects\Fuse8Test\Fuse8Test\Models\DAL\DBContext.cs:строка 15
   в Fuse8Test.Controllers.HomeController.Index() в c:\users\itenberg\documents\visual studio 2017\Projects\Fuse8Test\Fuse8Test\Controllers\HomeController.cs:строка 17
   в lambda_method(Closure , ControllerBase , Object[] )
   в System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   в System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   в System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   в System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   в System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   в System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   в System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   в System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   в System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

我无法弄清楚发生了什么。有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

虽然称为命名查询......但它们仍然与我们的ORM世界有关。我们需要 CLASS (甚至基本)映射。例如添加这个,一切都会起作用:

<class name="AsmName.Models.OrderDetails" >
    <id name="OrderNumber" />
    <property name="OrderNumber" />
    <property name="OrderDate" />
    <property name="ProductCode" />
    <property name="ProductName" />
    <property name="Quantity" />
    <property name="Price" />
</class>