c# - NHibernate Sqldatetime must be between 1/1/1753 and 12/31/9999 -
i'm using nhibernate in mvc project. problem is, while m trying update object m getting following error.
sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm.
in debug mode see date property not null. set datetime nullable on mapping. i'm still getting sqldatetime error.
public class entitybasemap<t> : classmap<t> t : entitybase { public entitybasemap() { id(x => x.id).generatedby.identity(); map(x => x.isdeleted).not.nullable(); map(x => x.createdat).nullable(); map(x => x.updatedat).nullable(); map(x => x.random).formula("newid()"); references(x => x.status).column("statusid"); where("isdeleted=0"); } }
datetime properties not null on save.
public int saveorupdatepage(pagedto pagedto) { var page = pagedto.id > 0 ? byid<page>(pagedto.id) : new page(); var status = pagedto.status.id > 0 ? lookupbyid<status>(pagedto.status.id) : null; var type = pagedto.type.id > 0 ? lookupbyid<pagetype>(pagedto.type.id) : null; //var parent = pagedto.parent.id > 0 ? byid<page>(pagedto.parent.id) : page.parent; page.description = pagedto.description; page.title = pagedto.title; page.spottext = pagedto.spottext; page.status = status; page.text = pagedto.text; page.url = !string.isnullorempty(pagedto.url) ? pagedto.url.tofilename() : pagedto.title.tofilename(); page.featured = pagedto.featured; page.type = type; using (var tran = unitofwork.currentsession.begintransaction()) { unitofwork.currentsession.saveorupdate(page); tran.commit(); } page.createdirectory(); if (pagedto.id == 0) { page.copy(); page.copythumbs(); } setresultassuccess(); return page.id; }
i m using sqlserver 2008 , on table datetime columns check allow null.
this not issue of null
or nullable column. issue of c# default value of datetime
valuetype.
default(datetime) == new datetime(1,1,1) // 1st january of year 1
so, because createdat or updatedat defined as
public virtual datetime createdat { get; set; } public virtual datetime updatedat { get; set; }
and never set other default value ... value 1.1.0001. , less sql server lower bound 1.1.1753...
other words, sure set these values meaningful values, e.g. datetime.now
Comments
Post a Comment