title: How to handle updating entities. NHibernate + ASP.NET MVC
body: <p>I cannot update created previously entity. I'm getting a <code>StaleObjectException</code> exception with message:</p>
  
  <p><em><code>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Project.DomainLayer.Entities.Employee#00000000-0000-0000-0000-000000000000]</code></em></p>
  
  <p>I don't share the update process with anyone. What's the problem?</p>
  
  <p><strong>Data Access / DI</strong></p>
  
  <pre><code>public class DataAccessModule : Ninject.Modules.NinjectModule
  {
      public override void Load()
      {
          this.Bind&lt;ISessionFactory&gt;()
              .ToMethod(c =&gt; new Configuration().Configure().BuildSessionFactory())
              .InSingletonScope();
  
          this.Bind&lt;ISession&gt;()
              .ToMethod(ctx =&gt; ctx.Kernel.TryGet&lt;ISessionFactory&gt;().OpenSession())
              .InRequestScope();
  
          this.Bind(typeof(IRepository&lt;&gt;)).To(typeof(Repository&lt;&gt;))
              .InRequestScope();
      }
  }
  </code></pre>
  
  <p><strong>Data Access / Mappings</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.DomainLayer"   namespace="Project.DomainLayer.Entities"&gt;
  &lt;class name="Employee" optimistic-lock="version"&gt;
      &lt;id name="ID" column="EmployeeID" unsaved-value="00000000-0000-0000-0000-000000000000"&gt;
          &lt;generator class="guid.comb" /&gt;
      &lt;/id&gt;
      &lt;version name="Version" type="Int32" column="Version" /&gt;
      &lt;!-- properties --&gt;
      &lt;property name="EmployeeNumber" /&gt;
      &lt;!-- ... --&gt;
      &lt;property name="PassportRegistredOn" not-null="true" /&gt;
      &lt;!-- sets --&gt;
      &lt;set name="AttachedInformation" cascade="all"&gt;
          &lt;key column="EmployeeID" /&gt;
          &lt;element column="Attachment" /&gt;
      &lt;/set&gt;
      &lt;set name="TravelVouchers" cascade="all"&gt;
          &lt;key column="EmployeeID" /&gt;
          &lt;one-to-many class="TravelVoucher" /&gt;
      &lt;/set&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>Data Access / Repository</strong></p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : AbstractEntity&lt;T&gt;, IAggregateRoot
  {
      private ISession session;
  
      public Repository(ISession session)
      {
          this.session = session;
      }
  
      // other methods are omitted
  
      public void Update(T entity)
      {            
          using(var transaction = this.session.BeginTransaction())
          {
              this.session.Update(entity);
              transaction.Commit();
          }
      }
      public void Update(Guid id)
      {            
          using(var transaction = this.session.BeginTransaction())
          {
              this.session.Update(this.session.Load&lt;T&gt;(id));
              transaction.Commit();
          }
      }
  } 
  </code></pre>
  
  <p><strong>Inside a Controller</strong></p>
  
  <pre><code>public class EmployeeController : Controller
  {
      private IRepository&lt;Employee&gt; repository;
  
      public EmployeeController(IRepository&lt;Employee&gt; repository)
      {
          this.repository = repository;
      }        
      public ActionResult Edit(Guid id)
      {
          var e = repository.Load(id);
          return View(e);
      }
      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Edit(Employee employee)
      {
          if(ModelState.IsValid)
          {
              repository.Update(employee);
              return RedirectToAction("Deatils", "Employee", new { id = employee.ID });
          }
          else
          {
              return View(employee);
          }
      }
  }
  </code></pre>
  
  <p>How do I update my entities?
  Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So I added <code>unsaved-value="{Guid.Empty goes here}"</code> to my markup. Moreover I've tried to do the next thing:</p>
  
  <pre><code>public void Update(T entity)
  {
      using(var transaction = this.session.BeginTransaction())
      {
          try
          {
              this.session.Update(entity);
              transaction.Commit();
          }
          catch(StaleObjectStateException ex)
          {
              try
              {
                  session.Merge(entity);
                  transaction.Commit();
              }
              catch
              {
                  transaction.Rollback();
                  throw;
              }
          }
  
      }
  }
  </code></pre>
  
  <p>And this gives me the same effect.. I mean <code>transaction.Commit();</code> after <code>Merge</code> gives the same exception.</p>
  
  <p>Also I'm wondering should I expose, using hidden input, the entity <code>ID</code> on the <code>Edit</code> view?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So entity really detaches. When it passes to controller the <code>ID</code> equals <code>Guid.Empty</code>. How do I handle it, <code>Merge</code> or <code>Reattach</code>?</p>