I'm using NHibernate for the first time and struggling. On web page one, I start a form. I want to save and navigate to page two. How do I update my RequestForm object with the ID of the newly inserted row? It is still 0 when I call the redirect, though the data was persisted to the DB.
Here's my controller code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult New(RequestForm form)
{
// contract
if (form == null)
{
throw new ArgumentException("Cannot be null", "form");
}
// validate the form
form.Validate(ModelState);
// if valid, save & continue
if (ModelState.IsValid)
{
var request = new Request();
form.Update(request);
repository.Save(request);
return RedirectToAction("Edit", "Request", new {id = form.ID});
}
// if not valid, return new form
return View("New", form);
}
Here is the repository code:
public void Save(T entity)
{
using (ISession session = GetSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(entity);
transaction.Commit();
}
}
Here's the relevant area of the mapping file:
<class
name="Request"
table="Data_Requests"
dynamic-update="true">
<id
name="Id"
type="Int32"
access="property"
column="RequestId"
unsaved-value="0">
<generator class="native"/>
</id>
Thanks for reading my question. I'm sure the answer is easy once you know what you are doing :)