title: Version as timestamp in Fluent NHibernate / SQL Server
body: <p>Using FNH w/ SQL Server 2008, I'm trying to add a Version as a timestamp, but running into the SQLDateTime Overflow error because the value is passed as 1/1/0001 12:00:00 AM. I found <a href="http://nhforge.org/doc/nh/en/index.html#mapping-types-basictypes" rel="nofollow noreferrer">this</a> (also referenced <a href="https://stackoverflow.com/questions/2033630/datetime-precision-in-nhibernate-and-support-for-datetime2-in-nhibernate-schemeex/2317854#2317854">here</a>), but still experiencing the problem.</p>
  
  <pre><code>// entity base
  public abstract class EntityBase
  {
      public virtual Int64 Id { get; set; }
      public virtual DateTime Version { get; set; }
  }
  
  // entity base map
  public abstract class EntityBaseMap&lt;T&gt; : ClassMap&lt;T&gt; where T : EntityBase
  {
      public EntityBaseMap()
      {
          Id(x =&gt; x.Id).GeneratedBy.Identity();
          OptimisticLock.Version();
          Version(x =&gt; x.Version)
             .CustomType("Timestamp");
  
      }
  }    
  </code></pre>
  
  <p>The SQL Server data type is "datetime". </p>
  
  <p>I'm guessing its something small and stupid, but haven't found the cause yet - what am I missing?</p>
  
  <p><strong>EDIT:</strong> Action method for the actual "save" code</p>
  
  <pre><code>    public ActionResult Create()
      {
          int currMaxSortOrder = session.CreateCriteria(typeof(Section))
                              .SetProjection(Projections.ProjectionList().Add(Projections.Max("Sortorder")))
                              .UniqueResult&lt;int&gt;();
          SectionViewModel sectionViewModel = new SectionViewModel();
          sectionViewModel.Sortorder = currMaxSortOrder + 1;
          return View("Create", "_AdminLayout", sectionViewModel);
      }
  
      [HttpPost]
      public ActionResult Create(SectionViewModel sectionInputModel)
      {
          if (ModelState.IsValid)
          {
              section = new Section();
              Mapper.Map(sectionInputModel, section);
              using (var tx = session.BeginTransaction())
              {
                  session.SaveOrUpdate(section);
          tx.Commit();
              }
              return RedirectToAction("index", "pages").WithFlash(new { success = "Section '" + section.Name + "' was successfully added." });
          }
          return View("Create", "_AdminLayout", section);
      }
  </code></pre>
  
  <p><strong>Edit 2:</strong> Added section entity &amp; mapping</p>
  
  <pre><code>    public class Section : EntityBase
      {
          public virtual String Name { get; set; }
          public virtual int Sortorder { get; set; }
          public virtual String RedirectUrl { get; set; }
          public virtual IList&lt;Page&gt; Pages { get; set; }
  
          public Section()
          {
              Pages = new List&lt;Page&gt;();
          }
  
          public virtual void AddPage(Page page)
          {
              page.Section = this;
              this.Pages.Add(page);
          }
      }
  
      public class SectionMap : EntityBaseMap&lt;Section&gt;
      {
          public SectionMap()
          {
              Map(x =&gt; x.Name);
              Map(x =&gt; x.Sortorder);
              Map(x =&gt; x.RedirectUrl);
              // one to many relationship
              HasMany(x =&gt; x.Pages)
                  .Inverse()
                  .Cascade.All();
          }
      }
  }
  </code></pre>