NHibernate Sqldatetime must be between 1/1/1753 and 12/31/9999

I am using NHibernate in my MVC project. My problem is when I try to update an object, I get the following error.

Overflow SqlDateTime. Must be between 1/1/1753 12:00:00 and 12/31/9999 11:59:59 PM.

In debug mode I see that the date property is not null. I was setting the Datetime value to display. But I am still getting sqldatetime error.

public class EntityBaseMap<T> : ClassMap<T> where 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");
    }
}

      

Date and time properties are not preserved when saved.

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 am using SQLServer 2008 and null resolution datetime table columns columns.

+3


source to share


1 answer


This is not a column problem NULL

or NULLABLE. This is the C # default value problem for DateTime

ValueType.

default(DateTime) == new DateTime(1,1,1) // 1st January of year 1 

      

So, since CreateAt or updatedAt is defined as



public virtual DateTime CreatedAt { get; set; }
public virtual DateTime UpdatedAt { get; set; }

      

And never set a value other than its default ... which value is 1.1.0001. And that's less than the lower bound of SQL Server 1.1.1753 ...

In other words, make sure you set these values โ€‹โ€‹to some meaningful values, for example. DateTime.Now

+6


source







All Articles