将TimeSpan属性保留为时间类型Mysql列时出错

时间:2012-07-03 15:37:35

标签: nhibernate fluent-nhibernate

我有一个TimeSpan属性,我希望使用Nhibernate持久保存到DB。 Mysql列类型是时间。我读到了那个

  

CustomType( “TimeAsTimeSpan”)

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

  

session.Save(对象)

将导致以下MySqlException

  

MySqlTimeSpan错误

只能序列化TimeSpan对象

我尝试保留的时间戳属性是有效的时间戳。有什么想法吗?

2 个答案:

答案 0 :(得分: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)'\'';
        }
    }
}

答案 1 :(得分:0)

好吧,因为没有人回答我的问题,我尝试了许多不同的方法而没有结果,我决定继续解决。我在插入varchar列时将时间戳对象转换为字符串类型(不再是时间类型)并在从DB读取时将字符串强制转换回时间戳对象。以下是转换的代码,以防万一。

  
    

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