NHibernate:如何调用模型的存储过程和单/复数?
谁能告诉我使用 NHibernate 调用存储过程的最佳和推荐方法?
我已经设置了映射,目前我已经可以正常工作了
var list = session.CreateSQLQuery("exec usp_Test").AddEntity(typeof(Product)).List<Product>();
,果然我得到了我的产品列表,但这是最好的方法吗?
还有模型类,即。产品最好设置为单数或复数(产品)。
提前致谢
Can anyone tell me the best and recommended way of calling a stored procedure using NHibernate?
I have my mappings set up and currently i have this working
var list = session.CreateSQLQuery("exec usp_Test").AddEntity(typeof(Product)).List<Product>();
And sure enough i am getting a LIST back of my products, but is this the best way of doing this?
Also are the Model classes ie. Product best set as Singular or Plural (Products).
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议尽可能避免使用 NHibernate 存储过程。
可能仍然有理由使用存储过程。是的,有几种方法可以将它们与 NHibernate 一起使用。
直接将它们作为查询调用(就像您所做的那样),将它们指定为预定义查询,或者您甚至可以将它们定义为 NHibernate 选择、插入或更新实体的方式。
有一篇来自 Ayende 的帖子如何在映射中使用存储过程文件。
Codeproject 上有一篇帖子介绍了如何从代码中调用它们。
关于您的代码:
AddEntity
不添加类型,而是添加实体实例。它实际上转向了它的主键。我不明白最后一句关于单数和复数的意思。这是另一个问题吗?
I suggest to avoid stored procedures with NHibernate whenever possible.
There might be still reasons to use stored procedures. Is so, there are several ways to use them with NHibernate.
Call them directly as a query (as you do), specify them as predefined query or you could even define them as the way how NHibernate should select, insert or update an entity.
There is a post from Ayende how to use stored procedures in mapping files.
There is a post on Codeproject how to call them from the code.
About your code:
AddEntity
does not add a type, but an entity instance. It is actually turned to its primary key.I don't understand the last sentence about singular and plural. Is this another question?
并不真地。通常,如果您使用 nHibernate,您可以允许它为您生成查询。并不是说你所做的事情有什么问题,而是你这样做是在欺骗自己,失去了 nHibernate 的一些用处。如果我是你,我会考虑通过使用标准对象来收集对象。
Not really. Typically if you're using nHibernate you allow it to generate queries for you. Not that there's anything wrong with what you're doing but you're cheating yourself out of some of the usefulness of nHibernate by doing it this way. If I were you I'd look into gathering objects through the use of criteria objects instead.