title: How to implement LastUpdate in NHibernate Entities?
body: <p>I'm attempting to implement an automatic LastUpdate attribute to my NHibernate entities. I thought about using database triggers and then thought it better done in NHibernate. I am following this <a href="http://www.shawnduggan.com/?p=89" rel="nofollow">blog post</a> as a guide.</p>
  
  <p>My domain entity is:</p>
  
  <pre><code>public class Instrument : Entity, IAuditableEntity
  {
      public virtual string Name { get;  set; }
      public virtual string Symbol {get;  set;}
      public virtual ISet&lt;BrokerInstrument&gt; BrokerInstruments { get; set; }
      public virtual bool IsActive { get; set; }        
  }
  </code></pre>
  
  <p>which inherits from </p>
  
  <pre><code>public abstract class Entity
  {
      public virtual int Id { get; set; }
      public virtual int Version {get; private set;}
      public virtual DateTime CreateDate { get; private set; }
      public virtual DateTime UpdateDate { get; set; }
  }
  </code></pre>
  
  <p>and here's the interface:</p>
  
  <pre><code>public interface IAuditableEntity
  {
      DateTime UpdateDate { get; set; }
  }
  </code></pre>
  
  <p>My listener class:</p>
  
  <pre><code>public class CustomUpdateEventListener : DefaultSaveEventListener
  {
      protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
      {
          var entity = evt.Entity as IAuditableEntity;
          if (entity != null)
          {
              ProcessEntityBeforeUpdate(entity);
          }
          return base.PerformSaveOrUpdate(evt);
      }
  
      internal virtual void ProcessEntityBeforeUpdate(IAuditableEntity entity)
      {
          entity.UpdateDate = DateTime.Now;
      }
  }
  </code></pre>
  
  <p>and my domain entity <code>Instrument</code> mapping file:</p>
  
  
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MooDB"  namespace="MooDB" &gt;
    &lt;class name="MooDB.Domain.Instrument,MooDB"  table="instruments"&gt;
      &lt;id name="Id" column="instrumentId" unsaved-value="0"&gt;
        &lt;generator class="increment"&gt;&lt;/generator&gt;
      &lt;/id&gt;
      &lt;version name="Version" column="version" type="integer" unsaved-value="0"/&gt;
      &lt;property name="Symbol" column="symbol" type="string" length="10" not-null="false"/&gt;
      &lt;property name="Name" column="`name`" type="string" length="30" not-null="false"/&gt;
      &lt;property name="IsActive" column="isActive" type="bool" not-null="true" /&gt;
      &lt;set name="BrokerInstruments" table="brokerInstruments" generic="true" inverse="true"&gt;
        &lt;key column="instrumentId" /&gt;
        &lt;one-to-many class="MooDB.Domain.BrokerInstrument"/&gt;
      &lt;/set&gt;
      &lt;property name="CreateDate" column="created" not-null="false" /&gt;
      &lt;property name="UpdateDate" column="lastUpdated" /&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>My hibernate.cfg.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" &gt;
    &lt;session-factory&gt;
    ... connection details snipped
      &lt;mapping assembly="MooDB"/&gt;
      &lt;listener class="MooDB.Data.CustomUpdateEventListener, MooDB.Data" type="update" /&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>My solution builds fine but I am getting a <code>FileNotFoundException</code> when NHibernate tries to find the listner. My references are set up correctly and I have the right <code>using</code> statements, so I'm not sure why NHibernate can't find it. Here's the error from the log:</p>
  
  <blockquote>
    <p>2012-01-15 15:26:12,911 [TestRunnerThread] ERROR
    NHibernate.Util.ReflectHelper [(null)] - Could not load type
    MooDB.Data.CustomUpdateEventListener, MooDB.Data.
    System.IO.FileNotFoundException: Could not load file or assembly
    'MooDB.Data' or one of its dependencies. The system cannot find the
    file specified. File name: 'MooDB.Data'</p>
  </blockquote>
  
  <p>Do you know what could be wrong?</p>
  
  <p><strong>Edit:</strong> I'm now getting the following <code>SqlTypeException</code>:</p>
  
  <p>2012-01-15 17:06:37,302 [TestRunnerThread] ERROR NHibernate.AdoNet.AbstractBatcher [(null)] - Could not execute command: UPDATE instruments SET version = @p0, symbol = @p1, [name] = @p2, isActive = @p3, created = @p4, lastUpdated = @p5 WHERE instrumentId = @p6 AND version = @p7 System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.</p>
  
  <p>It seems that the code <code>entity.UpdateDate = DateTime.Now;</code> in <code>ProcessEntityBeforeUpdate</code> is not being run for some reason. Any ideas?</p>