Domanda

helo,

Ho provato a creare un usertype che mappe dei due 'int' memorizzati in un database in un datetime. Il mio 'int' chiamato tempo e data nel database in modo tale da volerlo ottenere l'effetto desiderato.Ma ho ricevuto l'errore: il cast specificato non è valido.

Il mio codice:

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; }
    }
.

È stato utile?

Soluzione

La questione sembra essere risolta, come ad esempio dopo il debuging del lavoro correttamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top