将两个 Int 映射到 DateTime

发布于 2024-12-28 06:07:46 字数 2258 浏览 2 评论 0原文

你好,

我尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2025-01-04 06:07:46

事情似乎已经解决,等重新调试后工作正常。

The matter seems to be resolved, such as after re debuging work properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文