将两个 Int 映射到 DateTime
你好,
我尝试创建一个 UserType,将数据库中存储的两个“int”映射到日期时间。 我的“int”以我希望获得所需效果的方式调用数据库中的时间和日期。但我收到错误:指定的演员无效。
我的代码:
public class DateTimeUserType : ImmutableUserType
{
public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var date = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
var time = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[1]);
return Converts.ConvertDateTimeFromInt(date, time);
}
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var res = Converts.ConvertDateTimeToInt((DateTime)value);
NHibernateUtil.Int32.NullSafeSet(cmd, res.Date.GetValueOrDefault(), index);
NHibernateUtil.Int32.NullSafeSet(cmd, res.Time.GetValueOrDefault(), index + 1);
}
public override Type ReturnedType
{
get { return typeof(DateTime); }
}
public override SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.Int32, SqlTypeFactory.Int32 }; }
}
}
public abstract class ImmutableUserType : IUserType
{
public new virtual bool Equals(object x, object y)
{
return object.Equals(x, y);
}
public virtual int GetHashCode(object x)
{
return (x == null) ? 0 : x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public abstract object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(System.Data.IDbCommand cmd, object value, int index);
public abstract Type ReturnedType { get; }
public abstract SqlType[] SqlTypes { get; }
}
Helo,
I tried to create a UserType that maps of the two 'int' stored in a database to a Datetime.
My 'int's' called time and date in the database in such a way I wanted to get the desired effect. But I received the error: Specified cast is not valid.
My code:
public class DateTimeUserType : ImmutableUserType
{
public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var date = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
var time = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[1]);
return Converts.ConvertDateTimeFromInt(date, time);
}
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var res = Converts.ConvertDateTimeToInt((DateTime)value);
NHibernateUtil.Int32.NullSafeSet(cmd, res.Date.GetValueOrDefault(), index);
NHibernateUtil.Int32.NullSafeSet(cmd, res.Time.GetValueOrDefault(), index + 1);
}
public override Type ReturnedType
{
get { return typeof(DateTime); }
}
public override SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.Int32, SqlTypeFactory.Int32 }; }
}
}
public abstract class ImmutableUserType : IUserType
{
public new virtual bool Equals(object x, object y)
{
return object.Equals(x, y);
}
public virtual int GetHashCode(object x)
{
return (x == null) ? 0 : x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public abstract object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(System.Data.IDbCommand cmd, object value, int index);
public abstract Type ReturnedType { get; }
public abstract SqlType[] SqlTypes { get; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事情似乎已经解决,等重新调试后工作正常。
The matter seems to be resolved, such as after re debuging work properly.