save with smalldatetime field

I have an entity class with datetime field in it:

public virtual DateTime Date { get; set; }

With such mapping (NH 3.2 code mapping):

Property(l => l.Date, m =>
{
     m.Column("like_date");
     m.NotNullable(true);
     m.Type(NHibernateUtil.DateTime);
});

Column in MS SQL 2005 server is of SmallDatetime type (not nullable).

Problem is when I try to save this object, I get:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

Date is set correctly to Datetime.Now.


I turned out the problem was (as mentioned in other similar questions) not nullable DateTime property in class, which was nullable in database. The important part is, it wasn't about the class which I was saving, but other, which was fetched from db in the process.

If you run into such issue, check other classes too .

链接地址: http://www.djcxy.com/p/26970.html

上一篇: 如何获取NHibernate异常的更新SQL语句?

下一篇: 保存smalldatetime字段