views:

626

answers:

2

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.

+2  A: 

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.

Edit:

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.

Stuart Childs
"Make sure you are specifying the type explicitly on your mapping element" - how do i do that?
Chen Kinnrot
See the above edit.
Stuart Childs
do i need to specify type in fluent nhibernate?
Chen Kinnrot
A: 

Also, make sure you use nullables for your DateTimes that can be null in database.

DateTime? instead of just DateTime.

If not, NHibernate will attempt to initialize your date to a default value which probably isn't what you want.

jswanson