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

  • Home
  • SEARCH
  • 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 694131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:49:31+00:00 2026-05-14T02:49:31+00:00

I’m dealing with a legacy database that has date and time fields as char(8)

  • 0

I’m dealing with a legacy database that has date and time fields as char(8) columns (formatted yyyyMMdd and HH:mm:ss, respectively) in some of the tables. How can i map the 2 char columns to a single .NET DateTime property? I have tried the following, but i get a “can’t access setter” error of course because DateTime Date and TimeOfDay properties are read-only:

public class SweetPocoMannaFromHeaven
{    
    public virtual DateTime? FileCreationDateTime { get; set; }
}

.

mapping.Component<DateTime?>(x => x.FileCreationDateTime,
            dt =>
            {
                dt.Map(x => x.Value.Date,
                    "file_creation_date");
                dt.Map(x => x.Value.TimeOfDay,
                    "file_creation_time");
            });

I have also tried defining a IUserType for DateTime, but i can’t figure it out. I’ve done a ton of googling for an answer, but i can’t figure it out still. What is my best option to handle this stupid legacy database convention? A code example would be helpful since there’s not much out for documentation on some of these more obscure scenarios.

  • 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-14T02:49:31+00:00Added an answer on May 14, 2026 at 2:49 am

    You need an ICompositeUserType to handle more than one column. You need to beef up the error checking, parsing formats, etc, but here is a starting point for you.

    HTH,
    Berryl

    public class LegacyDateUserType : ICompositeUserType
    {
    
        public new bool Equals(object x, object y)
        {
            if (x == null || y == null) return false;
            return ReferenceEquals(x, y) || x.Equals(y);
        }
    
        public int GetHashCode(object x) {
            return x == null ? typeof (DateTime).GetHashCode() + 473 : x.GetHashCode();
        }
    
        public object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner)
        {
            if (dr == null) return null;
    
            var datePortion = NHibernateUtil.String.NullSafeGet(dr, names[0], session, owner) as string;
            var timePortion = NHibernateUtil.String.NullSafeGet(dr, names[1], session, owner) as string;
    
            var date = DateTime.Parse(datePortion);
            var time = DateTime.Parse(timePortion);
            return date.AddTicks(time.Ticks);
        }
    
        ///<summary>
        /// Write an instance of the mapped class to a prepared statement. Implementors 
        /// should handle possibility of null values. A multi-column type should be written 
        /// to parameters starting from index.
        ///</summary>
        public void NullSafeSet(IDbCommand cmd, object value, int index, ISessionImplementor session) {
            if (value == null) {
                // whatever
            }
            else {
                var date = (DateTime) value;
                var datePortion = date.ToString("your date format");
                NHibernateUtil.String.NullSafeSet(cmd, datePortion, index, session);
                var timePortion = date.ToString("your time format");
                NHibernateUtil.String.NullSafeSet(cmd, timePortion, index + 1, session);
            }
        }
    
        public object GetPropertyValue(object component, int property)
        {
            var date = (DateTime)component;
            return property == 0 ? date.ToString("your date format") : date.ToString("your time format");
        }
    
        public void SetPropertyValue(object component, int property, object value)
        {
            throw new NotSupportedException("DateTime is an immutable object.");
        }
    
        public object DeepCopy(object value) { return value; }
    
        public object Disassemble(object value, ISessionImplementor session) { return value; }
    
        public object Assemble(object cached, ISessionImplementor session, object owner) { return cached; }
    
        public object Replace(object original, object target, ISessionImplementor session, object owner) { return original; }
    
        ///<summary>Get the "property names" that may be used in a query.</summary>
        public string[] PropertyNames { get { return new[] { "DATE_PORTION", "TIME_PORTION" }; } }
    
        ///<summary>Get the corresponding "property types"</summary>
        public IType[] PropertyTypes { get { return new IType[] { NHibernateUtil.String, NHibernateUtil.String }; } }
    
        ///<summary>The class returned by NullSafeGet().</summary>
        public Type ReturnedClass { get { return typeof(DateTime); } }
    
        ///<summary>Are objects of this type mutable?</summary>
        public bool IsMutable { get { return false; } }
    
    }
    

    === fluent mapping (assuming automapping w/override classes) ====

     public void Override(AutoMapping<MyClass> m)
     {
         ....
         m.Map(x => x.MyDateTime).CustomType<LegacyDateUserType>();
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.