3

我有一个 TimeSpan 属性,我想使用 Nhibernate 将其持久化到 DB。Mysql 列类型是时间。我读了一遍

CustomType("TimeAsTimeSpan")

应该解决问题,但它没有。

session.Save(对象)

将导致以下 MySqlException

MySqlTimeSpan 错误只能序列化 TimeSpan 对象

我试图坚持的时间戳属性是一个有效的时间戳。有什么建议吗?

4

2 回答 2

2

这是 NHibernate 中的一个错误。我刚刚通过创建 TimeAsTimeSpan 类的克隆来解决这个问题。

用法:

CustomType(typeof(TimeAsTimeSpanTypeClone));

班级:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NHibernate.Type;

namespace DataAccess.NhibernateFixes
{
    /// <summary>
    /// Clone of the Nhibernate class: NHibernate.Type.TimeAsTimeSpanType
    /// This one actually works though...
    /// </summary>
    [Serializable]
    public class TimeAsTimeSpanTypeClone : PrimitiveType, IVersionType, IType, ICacheAssembler
    {
        private static readonly DateTime BaseDateValue = new DateTime(1753, 1, 1);

        public override string Name
        {
            get
            {
                return "TimeAsTimeSpan";
            }
        }

        public override System.Type ReturnedClass
        {
            get
            {
                return typeof(TimeSpan);
            }
        }

        public IComparer Comparator
        {
            get
            {
                return (IComparer)Comparer<TimeSpan>.Default;
            }
        }

        public override System.Type PrimitiveClass
        {
            get
            {
                return typeof(TimeSpan);
            }
        }

        public override object DefaultValue
        {
            get
            {
                return (object)TimeSpan.Zero;
            }
        }

        static TimeAsTimeSpanTypeClone()
        {
        }

        public TimeAsTimeSpanTypeClone()
            : base(SqlTypeFactory.Time)
        {
        }

        public override object Get(IDataReader rs, int index)
        {
            try
            {
                object obj = rs[index];
                if (obj is TimeSpan)
                    return (object)(TimeSpan)obj;
                else
                    return (object)((DateTime)obj).TimeOfDay;
            }
            catch (Exception ex)
            {
                throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[index]), ex);
            }
        }

        public override object Get(IDataReader rs, string name)
        {
            try
            {
                object obj = rs[name];
                if (obj is TimeSpan)
                    return (object)(TimeSpan)obj;
                else
                    return (object)((DateTime)obj).TimeOfDay;
            }
            catch (Exception ex)
            {
                throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[name]), ex);
            }
        }

        public override void Set(IDbCommand st, object value, int index)
        {
            DateTime dateTime = TimeAsTimeSpanTypeClone.BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
            ((IDataParameter)st.Parameters[index]).Value = (object)dateTime.TimeOfDay; // <<<<  fix here. Added ".TimeOfDay"
        }

        public override string ToString(object val)
        {
            return ((TimeSpan)val).Ticks.ToString();
        }

        public object Next(object current, ISessionImplementor session)
        {
            return this.Seed(session);
        }

        public virtual object Seed(ISessionImplementor session)
        {
            return (object)new TimeSpan(DateTime.Now.Ticks);
        }

        public object StringToObject(string xml)
        {
            return (object)TimeSpan.Parse(xml);
        }

        public override object FromStringValue(string xml)
        {
            return (object)TimeSpan.Parse(xml);
        }

        public override string ObjectToSQLString(object value, NHibernate.Dialect.Dialect dialect)
        {
            return (string)(object)'\'' + (object)((TimeSpan)value).Ticks.ToString() + (string)(object)'\'';
        }
    }
}
于 2014-09-25T14:45:58.967 回答
0

Well since no one answered my question and I tried many different approaches with no result, I decided to go with a work around.I converted my timestamp object to string when inserting to a varchar column Type (No longer a Time Type) and will cast the string back to a timestamp object when reading from the DB. Here is the code for that conversion just in case.

DateTime.Parse(startTimebtn.Text).TimeOfDay.ToString();

于 2012-07-09T13:13:45.940 回答