Time and Nhibernate


hello i got an application that uses nhibernate as orm, i need to store data that represents time,
whats the best way to do it?

nhibenate dont know to convert time field from db to a timespan, only string.

Best Solution

NHibernate supports DateTime, Ticks, TimeSpan and Timestamp. Make sure you are specifying the type explicitly on your mapping element as the different time types have different semantics so what NHibernate is guessing may not be correct.

If you are and are still having problems, modify your post to include the relevant portions of your entity, mapping file, and the actual problem you are encountering.


For example, with the following class for a TimeSpan:

public class MyClass
    // Other properties
    // ...
    // ...
    public virtual TimeSpan MyTimeProperty { get; set; }

And the mapping file:

<!-- other properties -->
<property name="MyTimeProperty" type="TimeSpan" /> <!-- Note: NH expects the DB type to be DbType.Int64 -->

You indicate that you're trying to map a TimeSpan ("nhibenate dont know to convert time field from db to a timespan, only string"). If this is the correct type matching between .NET (typeof TimeSpan) and the database (DbType.Int64), NH should do this automatically (i.e. you shouldn't need to specify type="TimeSpan"). So if it's not working, I suspect there is a problem with the way you have things setup. It may be helpful if you post the property/field declaration with full signature, the <property> line for this property from your mapping file, and the column definition from the database.

Related Question