我有一個TimeSpan屬性,我想使用Nhibernate將它保存到數據庫中。 Mysql列類型是Time。我看周圍的將TimeSpan屬性保留爲時間類型Mysql列的錯誤
CustomType( 「TimeAsTimeSpan」)
應該解決的問題,但它不。
session.Save(對象)
將導致以下個MySqlException
只有時間跨度目的可以通過MySqlTimeSpan錯誤
,即時通訊的時間戳屬性被序列化試圖堅持是一個有效的時間戳。任何sudggestions?
我有一個TimeSpan屬性,我想使用Nhibernate將它保存到數據庫中。 Mysql列類型是Time。我看周圍的將TimeSpan屬性保留爲時間類型Mysql列的錯誤
CustomType( 「TimeAsTimeSpan」)
應該解決的問題,但它不。
session.Save(對象)
將導致以下個MySqlException
只有時間跨度目的可以通過MySqlTimeSpan錯誤
,即時通訊的時間戳屬性被序列化試圖堅持是一個有效的時間戳。任何sudggestions?
好,因爲沒有人回答我的問題,我嘗試了許多不同的方法,沒有結果,我決定去解決。我插入到varchar列時將我的timestamp對象轉換爲字符串Type(不再是時間類型),並且在從DB讀取時將字符串轉換回時間戳對象。以防萬一,這是轉換的代碼。
DateTime.Parse(startTimebtn.Text).TimeOfDay.ToString();
這是NHibernate的一個bug。我剛剛設法通過創建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)'\'';
}
}
}
工作非常出色,非常感謝! – Latrova