我使用NHibernate和Postgresql作为后端,必须创建用于将System.DateTime转换为Postgresql“time”类型以及将System.timepans转换为“interval”db类型的自定义类型。我创建的IUserType正在工作,并且正在应用于读取和更新,但当我试图将对象插入数据库时,它们从未应用。我已经在IUserTypes中设置了断点,但是在发生插入时它们永远不会被命中。
我认为问题可能在于对象只是一个POCO对象,还不是代理,所以当我设置属性值时,应用转换到usertype的映射不会发生。
如果我执行更新并设置这些属性之一,我可以看到IUserType中的断点被触发。
有什么想法吗?
更新
正在调用IUserType的Equals方法,但我在其中进行必要转换的NullSafeSet不是。
编辑
添加了代码示例

    public class TimeType : BaseImmutableUserType<TimeSpan>
    {
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var val = NHibernateUtil.Time.NullSafeGet(rs, names[0]);
            if (val == null)
                return null;

            var dt = DateTime.Parse(val.ToString());

            return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
        }

        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var obj = (TimeSpan)value;

            ((IDbDataParameter) cmd.Parameters[index]).Value = obj;
        }

        public override SqlType[] SqlTypes
        {
            get
            {
                return new[] { SqlTypeFactory.Time };
            }
        }
    }

    public abstract class BaseImmutableUserType<T> : IUserType
    {
        public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
        public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
        public abstract SqlType[] SqlTypes { get; }

        public BaseImmutableUserType()
        {
            int i = 0;
        }

        public new bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }

            if (x == null || y == null)
            {
                return false;
            }

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        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 DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public Type ReturnedType
        {
            get { return typeof(T); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
}

最佳答案

我找到了以下两个选项:
选项1:不要使用自己的用户类型。相反,使用NHibernate自己的TimeAsTimeSpan如下:

Map(x => x.TimeFrom)
    .CustomType("TimeAsTimeSpan");

(示例取自here
选项2:稍微修改类:
public class TimeType : BaseImmutableUserType<TimeSpan>
{
    // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
    private static readonly DateTime BaseDateValue = new DateTime(1753, 01, 01);

    public override object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var val = NHibernateUtil.TimeAsTimeSpan.NullSafeGet(rs, names[0]);
        if (val == null)
            return null;

        var dt = DateTime.Parse(val.ToString());

        return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
    }

    public override void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        //var obj = (TimeSpan)value;  // we can't use TimeSpan here but need to use DateTime
        // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
        DateTime date = BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
        ((IDbDataParameter)cmd.Parameters[index]).Value = date;
    }

    public override SqlType[] SqlTypes
    {
        get
        {
            return new[] { NHibernate.SqlTypes.SqlTypeFactory.Time };
        }
    }
}

08-27 04:52