NHibernate:如何调用模型的存储过程和单/复数?

发布于 2024-10-01 08:31:15 字数 279 浏览 0 评论 0原文

谁能告诉我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2024-10-08 08:31:15

我建议尽可能避免使用 NHibernate 存储过程。

可能仍然有理由使用存储过程。是的,有几种方法可以将它们与 NHibernate 一起使用。

直接将它们作为查询调用(就像您所做的那样),将它们指定为预定义查询,或者您甚至可以将它们定义为 NHibernate 选择、插入或更新实体的方式。

有一篇来自 Ayende 的帖子如何在映射中使用存储过程文件。

Codeproject 上有一篇帖子介绍了如何从代码中调用它们。

关于您的代码:

  • AddEntity 不添加类型,而是添加实体实例。它实际上转向了它的主键。
  • 存储过程无法真正提供产品列表。只有NHibernate(ORM)能够从数据库读取对象。当您调用存储过程时,您直接访问数据库。

我不明白最后一句关于单数和复数的意思。这是另一个问题吗?

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.
  • The stored procedure can't really provide a list of Products. Only NHibernate, the ORM, is able to read objects from the database. When you call a stored procedure, you directly go to the database.

I don't understand the last sentence about singular and plural. Is this another question?

假面具 2024-10-08 08:31:15

并不真地。通常,如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文