Fluent NHibernate Retrieves the ID of a Stored Object

I am using Fluent NHibernate in an Asp.net MVC application. I configured it to start a session and transaction for each request and made a transaction on the request side. However, I want to save the object (in this case, the new "Company"), and then redirect the information about the company to this new page. How to get the ID of a new company so that I can redirect? If I get the identifier after the .Save (company) session, it is zero. This makes sense because it has not yet been completed, however, it still seems that there should be a relatively simple way to do this without completing the current transaction and creating a new one.

+5
source share
5 answers

OK, the problem was elsewhere in my code. You can get the identifier immediately after saving, even if you have not completed a transaction or reset it.

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

+2
source

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

If the id generator needs access to the database, this will happen at this time. Thus, for the Identity generator, insertion will be performed at this time, like any pending insertions.

Otherwise, the insertion will be delayed until the session is reset, which will happen:

  • Find() Enumerable()
  • NHibernate.ITransaction.Commit()
  • ISession.Flush()
+7

, ? , Session.Save

0

NHibernate , , INSERT - . Do Session.Flush() - , (INSERT) . . - (, , S # arp Architecture [Transaction]).

0

, . session-per-request, Session_EndRequest. , , - , EndRequest -, .

(), , .

0

All Articles