NHibernate and the loss of the miliseconds

Posts   
 
    
Gerderich
User
Posts: 3
Joined: 22-Jul-2013
# Posted on: 22-Jul-2013 19:46:02   

We are using LLBLGen Pro 3.5 Final as a mapping tool between NHibenate and SQL Server 2005. By default, when you have a mapped DateTime property on an entity, if you don’t specify any type, NHibernate will use the NHibernate.Type.DateTimeType type. Unfortunately, with this type, you lose the milliseconds.The workaround is to specify type NHibernate.Type.TimestampType. If we do not use Version fields, we got the problem, that NHibenate saves the datetime-values without milliseconds in the database, wich is not sufficient for our needs. How can we bring LLBLGen Pro to map datetime columns to the NHibernate.Type.TimestampType type?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Jul-2013 23:42:17   

I think you need to use dateTime2, right? Please check this thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=20472

Gerderich
User
Posts: 3
Joined: 22-Jul-2013
# Posted on: 23-Jul-2013 01:43:41   

Thank you Walaa for your fast reply. I had seen this thread bevor and it's dealing with the same problem, but we are using SQL Server 2005 wich had no such type and we are using fluent mapping. DateTime-columns are mapped like this:

Map(x=>x.DateTimeValue).Column("[DateTimeValue]").Access.CamelCaseField(Prefix.Underscore);

What I need is (LLBLGen uses this mapping for its version-fields):

Map(x => x.DateTimeValue).Column("[DateTimeValue]").Access.CamelCaseField(Prefix.Underscore).CustomType("Timestamp");

This mapps the DateTime-Type to the NHibernate.Type.TimestampType type. I changed the mapping by hand and it works. In C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.5\Frameworks\NHibernate\Templates\Net3.5\C#\entityFluentMapping.lpt I found code that probably generates the mapping-code, but I have no idea, how I need to change it. Maybe I need to write a custom template?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jul-2013 08:23:29   

I think this is NHibernate issue: http://devlicio.us/blogs/derik_whittaker/archive/2010/02/22/hey-nhibernate-give-me-back-my-milliseconds.aspx

What you can do, I think, is create a custom IUserType for that, or create your own override of the template, you can check the SDK documentation for that.

(Edit) Another way to solve this is to create a convention and use it in your own MySessionManager. That way, to don't have to worry about the code generation process to override your code. Example:

public class TimeStampPropertyConventions : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        Type type = instance.Property.PropertyType;
        if (type == typeof(DateTime) || type == typeof(DateTime?))
            instance.CustomType("Timestamp");
    }
}

public static partial class MySessionManager
{ 
    private static readonly ISessionFactory _sessionFactory;

    static MySessionManager()
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005
                        .ConnectionString(c => c.FromConnectionStringWithKey("NW.ConnectionString.SQL Server (SqlClient)")))
            .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(SessionManager).Assembly)
                .Conventions.Add(new TimeStampPropertyConventions()))
            .BuildSessionFactory();         
    }

    public static ISession OpenSession()
    {
        return _sessionFactory.OpenSession();
    }

    public static ISessionFactory SessionFactory
    {
        get { return _sessionFactory; }
    }
}
David Elizondo | LLBLGen Support Team
Gerderich
User
Posts: 3
Joined: 22-Jul-2013
# Posted on: 23-Jul-2013 11:22:37   

Thank you daelmo, I tested this approach and it works fine. I'm just waiting for the go from thebigboss and I will close this thread.