C# – Nullable DateTime in NHibernate 2.0.1

cdatetimenhibernatenullable

Two issues that will likely be immediately obvious, but I've spent too long already tracking them down:

  1. I've set up an AuditInterceptor in order to do automatic auditing on entities that implement my IAuditable interface which is used as follows:

    public class AuditInterceptor : EmptyInterceptor
    {
    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
    {
    var auditable = entity as IAuditable;

        if (auditable == null)
            return false;
    
        var now = DateTime.Now;
        auditable.CreateDate = now;
        auditable.ModifiedDate = now;
    
        return true;
    }
    
    public override bool OnFlushDirty(object entity, object id, object[] currentState,
        object[] previousState, string[] propertyNames, IType[] types)
    {
        var auditable = entity as IAuditable;
    
        if (auditable == null)
            return false;
    
        auditable.ModifiedDate = DateTime.Now;
    
        return true;
    }
    

    }

The two fields are both not nullable in the database, POCO, and mapping file. However, on Save, while the interceptor is being called (can step through it and see the applicable fields being set to DateTime.Now), there's still an SqlDateTime exception being thrown:

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

I've seen this before where there's been a mismatch somewhere (mapping file, database, POCO) where the DateTime is specified null in some and not in others. However, I've checked and it's all good on that front. I can manually set the values before SaveOrUpdate(entity) and it inserts/saves without problems. Without manually setting them, It's acting as if the interceptor is being missed altogether (even though I know it's not) and the two DateTime properties are never set (and therefore are treated as DateTime.MinValue: 01/01/0001…) and therefore out of range. But when stepping through, the values are properly set to DateTime.Now.

What is happening? For what it's worth, I had actually set up an event listener previously as well and it resulted in the same issue.

  1. Now for a dumb question that must have been answered out there somewhere, but I can't find it: As part of dealing with the above issue, I quickly checked to see whether making DateTime nullable throughout, but I know one of the breaking changes with NHibernate 2.* is that the NHibernate.Nullables are no longer supported. Therefore, what do you use in your mapping file to map the nullable DateTime? type? For i.e.:

Understandably doesn't work:

<property name="CreateDate" column="CreateDate" type="DateTime?" not-null="false" />

And no longer supported:

<property name="ModifiedDate" column="ModifiedDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"/>

Any pointers would be much appreciated!

Best Answer

This explains it:

if (!(entity is IAuditable))
    return false;

DateTime now = DateTime.Now;
state[Array.IndexOf(propertyNames, "CreateDate")] = now;
state[Array.IndexOf(propertyNames, "ModifiedDate")] = now;

return true;

But still wondering about the 2.* way to declare DateTime? in NHibernate mapping files.

[Update] Asked it in another question and got the answer: https://stackoverflow.com/questions/593236/nhibernate-2-mapping-files-how-to-define-nullable-datetime-type-datetime