Is there a practical way to use the hierarchyID datatype in entity framework 4?

前端 未结 1 955
孤城傲影
孤城傲影 2020-12-15 22:31

As it stands now, the CLR UDTs including HierarchyID aren\'t supported in Entity Framework 4. HierarchyID.ToString() is useful, but breaks down once any item has 10+ siblin

相关标签:
1条回答
  • 2020-12-15 23:29

    Well, I seem to be getting views but no responses. I had some immediate needs to work with the hierarchy structure above SQL, so i put together a static helper class. I don't consider this a complete solution, but so far it works relatively well. PadPath is really the critical function here.

    public static class SQLHierarchyManipulatin {
        const int   DEFAULT_PAD_LEN     = 3;
        const char  DEFAULT_PAD_CHAR    = '0';
    
        public static string PadPath(string Hierarchy) {
            return PadPath (Hierarchy, DEFAULT_PAD_LEN);
        }       
        public static string PadPath(string Hierarchy, int padLen) {
            string[]    components  = Hierarchy.Split('/');
    
            for (var i = 0; i < components.Length; i++ ) {
                if (components[i] != "") {
                    components[i] = components[i].PadLeft(padLen, DEFAULT_PAD_CHAR);
                }
            }
            return string.Join("/", components);
        }
    
        public static int CurrentNodeIndex(string Hierarchy) {
            string[]    components  = Hierarchy.Split('/');
            string      startItem   = components[components.Length - 2]; //one slot back from trailing slash
    
            return int.Parse(startItem);
        }
    
        public static string ParentPath (string Hierarchy) {
            return  Hierarchy.Substring(0, Hierarchy.TrimEnd('/').LastIndexOf('/') + 1);
        }
    
        public static string AppendChildWithPadding (string Hierarchy, int childIndex, int padLen) {
            return AppendChild(Hierarchy, childIndex, DEFAULT_PAD_LEN);
        }
        public static string AppendChildWithPadding (string Hierarchy, int childIndex) {
            return AppendChild(Hierarchy, childIndex, DEFAULT_PAD_LEN);
        }
        public static string AppendChild (string Hierarchy, int childIndex) {
            return AppendChild(Hierarchy, childIndex, DEFAULT_PAD_LEN);
        }
        public static string AppendChild (string Hierarchy, int childIndex, int padLen) {
            return Hierarchy + childIndex.ToString().PadLeft(padLen, DEFAULT_PAD_CHAR) + "/";
        }
    }
    

    Hope this helps someone! Although, I'd still like to hear from people.

    0 讨论(0)
提交回复
热议问题