c# – NHibernate IUserType未应用于Insert

我正在使用NHibernate和
Postgresql作为后端,并且必须创建自定义类型以将System.DateTime转换为Postgresql“time”类型以及System.TimeSpans转换为“interval”db类型.我创建的IUserType正在工作,正在应用于读取和更新,但是当我尝试将对象插入数据库时​​,它们永远不会被应用.我在IUserTypes中设置了断点,但是当插入发生时它们永远不会被击中.

我认为这个问题可能是因为对象只是一个POCO对象而且还不是代理,因此当我设置my属性的值时,不会发生将转换应用于usertype的映射.

如果我进行更新并设置其中一个属性,我可以看到IUserType中的断点被触发.

有什么想法吗?

UPDATE
正在调用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:不要使用您自己的UserType.而是像这样使用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 };
        }
    }
}
点赞