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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With