Fluent NHibernate 获取保存对象的 Id

发布于 2024-08-21 00:42:27 字数 246 浏览 3 评论 0原文

我在 Asp.net MVC 应用程序中使用 Fluent NHibernate。我将其设置为在每个请求上启动会话和事务,并在请求结束时提交事务。但是,我想要做的是保存一个对象(在本例中为一个新的“公司”),然后重定向到该新公司的详细信息页面。如何获取新公司的 ID 以便进行重定向?如果我在 session.Save(company) 之后得到 Id,它是空的。这是有道理的,因为它尚未提交,但是,似乎仍然应该有一种相对简单的方法来执行此操作,而无需提交当前事务并启动新事务。

I'm using Fluent NHibernate in an Asp.net MVC application. I have it set up to start a session and transaction on every request, and commit the transaction at the request end. However, what I want to do is save an object (in this case, a new "Company") and then redirect to that new Company's detail page. How do I get the Id of the new company so that I can redirect? If I get the Id after session.Save(company), it is null. This makes sense as it hasn't yet been committed, however, it still seems there should be a relatively easy way to do this without committing the current transaction and starting a new one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

红衣飘飘貌似仙 2024-08-28 00:42:27

当使用瞬态实体调用 session.Save() 或 session.SaveOrUpdate() 时,NHibernate 会生成一个 id。此时实体的id属性就设置好了并且可以使用了。

如果id生成器需要访问数据库,此时就会发生。因此,对于身份生成器,此时将执行插入,任何挂起的插入也将执行。

否则,插入将处于挂起状态,直到会话被刷新为止,这将发生:

  • ) 的 Find() 或 Enumerable() 的某些调用
  • 来自 NHibernate.ITransaction.Commit()
  • 和 ISession.Flush(

NHibernate generates an id when session.Save() or session.SaveOrUpdate() is called with an transient entity. At this time, the id property of the entity is set and can be used.

If the id generator requires database access, it will happen at this time. So for the Identity generator, the insert will be performed at this time as will any pending inserts.

Otherwise, the insert is pending until the session is flushed which will happen:

  • from some invocations of Find() or Enumerable()
  • from NHibernate.ITransaction.Commit()
  • from ISession.Flush()
绿萝 2024-08-28 00:42:27

好的,问题出在我的代码的其他地方。即使您尚未提交事务或刷新事务,您也可以在保存后立即获取 Id。

感谢大家的回答,但不幸的是,没有一个是完全正确的。

OK, the problem was elsewhere in my code. You can get the Id immediately after save, even if you haven't committed the transaction or flushed it.

Thanks everyone for the answers, but unfortunately none of them are entirely correct.

安稳善良 2024-08-28 00:42:27

也许它没有在数据库中设置为自动生成?正如 Mattias Jakobsson 所说,id 应该在 Session.Save 之后设置

Maybe it is not set as autogenerated in database? As Mattias Jakobsson sayed, id should be setted after Session.Save

悲歌长辞 2024-08-28 00:42:27

由于它是一个事务,NHibernate 推迟了更改,因此 INSERT 不会发送到数据库 - 并且数据库不会生成 ID。执行 Session.Flush() - 这不会停止事务,但您的更改(INSERT)将被执行。或者使用指定的 ID。或者不要总是启动事务 - 明确地执行此操作(例如 S#arp 架构使用 [Transaction] 属性执行此操作)。

Since it's a transaction NHibernate postpone changes and thus INSERT is not sent to database - and no ID is generated by the database. Do Session.Flush() - this won't stop transaction but your changes (INSERT) will be executed. Or use assigned IDs. Or don't always start transactions - do this explicitely (as for example S#arp Architecture does with [Transaction] attribute).

撩人痒 2024-08-28 00:42:27

我认为为每个会话启动事务是一个坏主意。我正在使用每个请求会话,并在 Session_EndRequest 中启动并提交事务。然而,我通常在页面上有一个事务,以便我可以处理那里发生的任何错误——当 EndRequest 发生时,除了显示通用错误消息之外,已经来不及做任何事情了。

如果您使用数据库生成的(身份)ID,那么您必须刷新会话才能生成 ID。

I think it's a bad idea to start a transaction for every session. I'm using session-per-request and I start and commit a transaction in Session_EndRequest. However, I generally have a transaction on the page so that I can handle any errors that occur there -- by the time EndRequest occurs it's too late to do anything but present a generic error message.

If you're using database generated (identity) IDs then you'll have to flush the session in order to generate the ID.

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