Version as a timestamp in Fluent NHibernate / SQL Server

Using FNH w / SQL Server 2008, I am trying to add the version as a timestamp, but it works in the SQLDateTime Overflow error because the value is passed as 1/1/0001 12:00:00 AM. I found this (also mentioned here ), but still experiencing a 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"); } } 

SQL Server data type "datetime".

I suppose this is something small and stupid, but have not yet found a reason - 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 object and section display

  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(); } } } 
0
source share
2 answers

shy doh! Moment

(adding this if any other n00bs like me get into the same problem)

Finally, I went deeper and realized that I had configured it to use AutoMapping while I was creating maps that would work only with FluentMapping. Canceled the use of FluentMapping, and the version began to work fine!

I suppose I can use AutoMapping and add an agreement that will handle a column named "Version" using CustomType ("Timestamp"), but for now I'm going to use FluentMapping, until I get more speed.

+2
source

This can be classic .NET min datetime! = SQL Server minimum datetime.

The minimum datetime in .NET is in the year 0001, but on a SQL server, the minimum date can only reach the year 1753. You get an overflow in SQL Server because the SQL datetime type cannot store the date you are trying to pass.

You might be lucky with the datetime2 type, but I'm not sure about compatibility with Hibernate.

See this article for more information: http://blog.malevy.net/2010/01/datetimeminvalue-sql-server-minimum.html

0
source

All Articles