SQL 2008 HierarchyID support in NHibernate

前端 未结 2 857
梦谈多话
梦谈多话 2020-12-16 11:43

Searched various NHibernate lists and haven\'t come up with a definitive answer. The SQL2008 dialect doesn\'t appear to have support for the HierarchyID data type - new dat

2条回答
  •  醉话见心
    2020-12-16 12:00

    I've given Needles' answer a test run. It's a very good answer but there are some changes needed to make it function (at least in .NET 4). Here's what I've come up with for my project:

    Update: the following code can be download over at GitHub and will be updated there. NHiberntate.HierarchyId.UserType

    SqlHierarchyId IUserType

    namespace NHibernate.UserTypes
    {
        using SqlTypes;
        using System;
        using System.Data;
        using System.Data.SqlTypes;
        using Microsoft.SqlServer.Types;
    
        public class HierarchyId : IUserType
        {
            #region Properties
    
            public SqlType[] SqlTypes
            {
                get { return new[] { NHibernateUtil.String.SqlType }; }
            }
    
            public Type ReturnedType
            {
                get { return typeof(SqlHierarchyId); }
            }
    
            public bool IsMutable
            {
                get { return true; }
            }
    
            #endregion Properties
    
            #region Methods
    
            new public 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 NullSafeGet(IDataReader rs, string[] names, object owner)
            {
                object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);
    
                if (prop1 == null) return null;
    
                return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
            }
    
            public void NullSafeSet(IDbCommand cmd, object value, int index)
            {
                if (value == null)
                    ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
    
                else if (value is SqlHierarchyId)
                    ((IDataParameter)cmd.Parameters[index]).Value = ((SqlHierarchyId)value).ToString();
            }
    
            public object DeepCopy(object value)
            {
                if (value == null) return null;
    
                return SqlHierarchyId.Parse(((SqlHierarchyId)value).ToString());
            }
    
            public object Replace(object original, object target, object owner)
            {
                return DeepCopy(original);
            }
    
            public object Assemble(object cached, object owner)
            {
                return DeepCopy(cached);
            }
    
            public object Disassemble(object value)
            {
                return DeepCopy(value);
            }
    
            #endregion Methods
        }
    }
    

    Mapping

    
    
        
    
            
            ...
    
        
    
    

    Object with HierarchyId

    namespace NHibernate.Map
    {
        using Microsoft.SqlServer.Types;
    
        public class OrganizationUnit
        {
            #region Fields
    
            private SqlHierarchyId _hierarchyId;
            ...
    
            #endregion Fields
    
            #region Properties
    
            public virtual SqlHierarchyId HierarchyId
            {
                get { return _hierarchyId; }
                set { _hierarchyId = value; }
            }
            ...
    
            #endregion Properties
        }
    }
    

提交回复
热议问题