我有一堂课:
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();我收到一个异常“命名查询中的错误:{GetOrdersByDate}”。内部异常为null。
堆栈跟踪为:
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()
我不知道发生了什么事。有谁能够帮助我?
最佳答案
虽然称为命名查询...它们仍然与我们的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>
关于c# - 命名查询中的NHibernate错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45327527/