Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
Chen Kinnrot Avatar asked Jun 03 '09 17:06

Chen Kinnrot


People also ask

What is the use of NHibernate?

NHibernate is an ORM (Object Relational Mapper). Its purpose is to map objects in your OO application to tables in a database for persistence. Why would you need it? Because it can save you from writing a lot of tedious ADO.NET code.

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

Is NHibernate an ORM?

NHibernate is a popular, fast growing ORM with a helpful community of seasoned developers. Used in thousands of commercial and open source projects.

What is NHibernate in .NET core?

NHibernate is the ORM, an object-relational platform that works on the ASP.Net core. Therefore, we can easily deploy the core project developed in ASP.NET that uses NHibernate on the Linux server without any effort. The application works in the same way as it will do on a windows platform.


1 Answers

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.

like image 57
Stuart Childs Avatar answered Oct 26 '22 23:10

Stuart Childs