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 this (also referenced here), but still experiencing the problem.
// 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<T> : ClassMap<T> where T : EntityBase
{
public EntityBaseMap()
{
Id(x => x.Id).GeneratedBy.Identity();
OptimisticLock.Version();
Version(x => x.Version)
.CustomType("Timestamp");
}
}
The SQL Server data type is "datetime".
I'm guessing its something small and stupid, but haven't found the cause yet - what am I missing?
EDIT: Action method for the actual "save" code
public ActionResult Create()
{
int currMaxSortOrder = session.CreateCriteria(typeof(Section))
.SetProjection(Projections.ProjectionList().Add(Projections.Max("Sortorder")))
.UniqueResult<int>();
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);
}
Edit 2: Added section entity & mapping
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<Page> Pages { get; set; }
public Section()
{
Pages = new List<Page>();
}
public virtual void AddPage(Page page)
{
page.Section = this;
this.Pages.Add(page);
}
}
public class SectionMap : EntityBaseMap<Section>
{
public SectionMap()
{
Map(x => x.Name);
Map(x => x.Sortorder);
Map(x => x.RedirectUrl);
// one to many relationship
HasMany(x => x.Pages)
.Inverse()
.Cascade.All();
}
}
}