将两个Int映射到DateTime

时间:2012-01-23 11:16:30

标签: nhibernate fluent-nhibernate mapping fluent-nhibernate-mapping

直升机,

我尝试创建一个UserType,将数据库中存储的两个'int'映射到Datetime。 我的'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; }
    }

1 个答案:

答案 0 :(得分:0)

此事似乎得到了解决,例如在重新调整工作之后。