Time and Nhibernate

Hi, I got an application that uses nhibernate as orm, I need to store data representing time, What is the best way to do this?

nhibenate does not know to convert a time field from db to timepan, just a string.

+6
nhibernate nhibernate-mapping
source share
2 answers

NHibernate supports DateTime, Ticks, TimeSpan and Timestamp . Make sure you explicitly point the type to your matching element, since different time types have different semantics, so NHibernate oversight may be wrong.

If you still have problems, modify the message to include the relevant parts of your object, the mapping file, and the actual problem you are facing.

Edit:

For example, with the following class for 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 are trying to match TimeSpan ("nhibenate dont know to convert a time field from db to timepan, only string"). If this is the correct type of correspondence between .NET ( typeof TimeSpan ) and the database ( DbType.Int64 ), NH should do this automatically (ie you do not need to specify type="TimeSpan" ). Therefore, if it does not work, I suspect that there is a problem with the way you have the settings. It may be useful if you publish a property / field declaration with a full signature, the <property> for this property from your mapping file, and the definition of the column from the database.

+7
source share

Also, make sure you use nullables for DateTimes, which may be empty in the database.

DateTime? not just DateTime.

If not, NHibernate will try to initialize your date with a default value, which is probably not what you want.

+2
source share

All Articles