Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 634079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:15:17+00:00 2026-05-13T20:15:17+00:00

Porting over an application to use NHibernate from a different ORM. I’ve started to

  • 0

Porting over an application to use NHibernate from a different ORM.

I’ve started to put in place the ability to run our unit tests against an in memory SQLite database. This works on the first few batches of tests, but I just hit a snag. Our app would in the real world be talking to a SQL 2008 server, and as such, several models currently have a DateTimeOffset property. When mapping to/from SQL 2008 in non-test applications, this all works fine.

Is there some mechanism either in configuring the database or some other facility so that when I use a session from my SQLite test fixture that the DateTimeOffset stuff is “auto-magically” handled as the more platform agnostic DateTime?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:15:17+00:00Added an answer on May 13, 2026 at 8:15 pm

    Coincidentally, I just hit this problem myself today 🙂 I haven’t tested this solution thoroughly, and I’m new to NHibernate, but it seems to work in the trivial case that I’ve tried.

    First you need to create an IUserType implementation that will convert from DateTimeOffset to DateTime. There’s a full example of how to create a user type on the Ayende blog but the relevant method implementations for our purposes are:

    public class NormalizedDateTimeUserType : IUserType
    {
        private readonly TimeZoneInfo databaseTimeZone = TimeZoneInfo.Local;
    
        // Other standard interface  implementations omitted ...
    
        public Type ReturnedType
        {
            get { return typeof(DateTimeOffset); }
        }
    
        public SqlType[] SqlTypes
        {
            get { return new[] { new SqlType(DbType.DateTime) }; }
        }
    
        public object NullSafeGet(IDataReader dr, string[] names, object owner)
        {
            object r = dr[names[0]];
            if (r == DBNull.Value)
            {
                return null;
            }
    
            DateTime storedTime = (DateTime)r;
            return new DateTimeOffset(storedTime, this.databaseTimeZone.BaseUtcOffset);
        }
    
        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                NHibernateUtil.DateTime.NullSafeSet(cmd, null, index);
            }
            else
            {
                DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
                DateTime paramVal = dateTimeOffset.ToOffset(this.databaseTimeZone.BaseUtcOffset).DateTime;
    
                IDataParameter parameter = (IDataParameter)cmd.Parameters[index];
                parameter.Value = paramVal;
            }
        }
    }
    

    The databaseTimeZone field holds a TimeZone which describes the time zone that is used to store values in the database. All DateTimeOffset value are converted to this time zone before storage. In my current implementation it is hard-coded to the local time zone, but you could always define an ITimeZoneProvider interface and have it injected into a constructor.

    To use this user type without modifying all my class maps, I created a Convention in Fluent NH:

    public class NormalizedDateTimeUserTypeConvention : UserTypeConvention<NormalizedDateTimeUserType>
    {
    }
    

    and I applied this convention in my mappings, as in this example (the new NormalizedDateTimeUserTypeConvention() is the important part):

    mappingConfiguration.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
                    .Conventions.Add(
                    PrimaryKey.Name.Is(x => x.EntityType.Name + "Id"), 
                    new NormalizedDateTimeUserTypeConvention(),
                    ForeignKey.EndsWith("Id"));
    

    Like I said, this isn’t tested thoroughly, so be careful! But now, all I need to do is to alter one line of code (the fluent mappings specification) and I can switch between DateTime and DateTimeOffset in the database.


    Edit

    As requested, the Fluent NHibernate configuration:

    To build a session factory for SQL Server:

    private static ISessionFactory CreateSessionFactory(string connectionString)
    {
        return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
                .Mappings(m => MappingHelper.SetupMappingConfiguration(m, false))
                .BuildSessionFactory();
    }
    

    For SQLite:

    return Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.InMemory)
                .Mappings(m => MappingHelper.SetupMappingConfiguration(m, true))
                .ExposeConfiguration(cfg => configuration = cfg)
                .BuildSessionFactory();
    

    Implementation of SetupMappingConfiguration:

    public static void SetupMappingConfiguration(MappingConfiguration mappingConfiguration, bool useNormalizedDates)
    {
        mappingConfiguration.FluentMappings
            .AddFromAssembly(Assembly.GetExecutingAssembly())
            .Conventions.Add(
                PrimaryKey.Name.Is(x => x.EntityType.Name + "Id"), 
                ForeignKey.EndsWith("Id"));
    
        if (useNormalizedDates)
        {
            mappingConfiguration.FluentMappings.Conventions.Add(new NormalizedDateTimeUserTypeConvention());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, but you aren't going to enjoy it. Replace all… May 14, 2026 at 11:07 pm
  • Editorial Team
    Editorial Team added an answer Well, paxdiablo has apparently summed it up here. But from… May 14, 2026 at 11:07 pm
  • Editorial Team
    Editorial Team added an answer SOLUTION: added "background-color:#FFFFFF;" to the element style. and used the… May 14, 2026 at 11:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.