Search code examples
c#sql-server-2008nhibernatenhibernate-mapping

NHibernate truncate milliseconds from DateTime variables


how can I solve this problema ? NHibernate truncate milliseconds from DateTime variables

DataHoraRecebimento = DateTime.Now;
DataReferencia = new DateTime(2015, 9, 8, 17, 0, 50, 332);

Even though I configure de LongTimePattern:

enter image description here

here is my configuration on hbm:

version 3.1.0.4000, xmlns="urn:nhibernate-mapping-2.2", MsSql2008Dialect

 <class name="MensagemRecebimento" table="dbo.xxx" lazy="true" dynamic-update="true">

    <id name="CodigoRecebimentoMensagem" column="COD_RECB_MSG"><generator class="identity" /></id>
    <property name="DataReferencia"><column name="DATA_REF_MSG" sql-type="datetime" not-null="true" /></property>
    <property name="DataHoraRecebimento"><column name="DTHR_RECB_MSG" sql-type="datetime" not-null="true" /></property>

  </class>

and here is the code that saves my object

NHibernateSessionHelper.Instance.GetSession().Save(obj);

and here is the result query that nHibernate generates after save you can notice that the milliseconds are zeros

NHibernate:
    INSERT
    INTO
        dbo.xxx
        (DATA_REF_MSG, DTHR_RECB_MSG)
    VALUES
        (@p7, @p8);
    select
        SCOPE_IDENTITY();

    @p7 = 08/09/2015 17:00:50.000 [Type: DateTime (0)],
    @p8 = 19/08/2016 12:42:48.000 [Type: DateTime (0)]

Solution

  • I got the answer, I added type="Timestamp" to the hbm.xml

    <class name="MensagemRecebimento" table="dbo.xxx" lazy="true" dynamic-update="true">
    
      <id name="CodigoRecebimentoMensagem" column="COD_RECB_MSG"> <generator class="identity" /> </id>
      <property name="DataReferencia" type="Timestamp"> <column name="DATA_REF_MSG" sql-type="datetime" not-null="true" /> </property>
      <property name="DataHoraRecebimento" type="Timestamp"> <column name="DTHR_RECB_MSG" sql-type="datetime" not-null="true" /></property>
    
    </class>