Sql server 在EntityFramework4中使用hierarchyID数据类型是否有一种实用的方法?
目前,实体框架4不支持包括HierarchyID在内的CLR UDT。HierarchyID.ToString()很有用,但一旦任何项有10+个同级,它就会崩溃(基本结构是/3/4/12/或/3/4/2/所以第12个节点将在第2个节点之前排序) 更多关于潜在选项的信息:Sql server 在EntityFramework4中使用hierarchyID数据类型是否有一种实用的方法?,sql-server,entity-framework,sql-server-2008,entity-framework-4,hierarchical-data,Sql Server,Entity Framework,Sql Server 2008,Entity Framework 4,Hierarchical Data,目前,实体框架4不支持包括HierarchyID在内的CLR UDT。HierarchyID.ToString()很有用,但一旦任何项有10+个同级,它就会崩溃(基本结构是/3/4/12/或/3/4/2/所以第12个节点将在第2个节点之前排序) 更多关于潜在选项的信息: 将hierarchyID作为varbinary返回,并实现我自己的二进制分类器 将hierarchyID作为varbinary返回,并实现我自己的hierarchyID.ToString()方法,该方法在构建字符串时用零填充数
- 将hierarchyID作为varbinary返回,并实现我自己的二进制分类器
- 将hierarchyID作为varbinary返回,并实现我自己的hierarchyID.ToString()方法,该方法在构建字符串时用零填充数字,以便生成的字符串可排序(即“/0003/0004/0012/”)。我取消了对Microsoft.SqlServer.Types.dll的分析,并查看了实现。看起来interal是基于一个名为“OrdTree”的类,我可以使用该类作为重新实现的基础
- 为SQL编写自己的CLR类型以处理二进制数据并构建自己的字符串(选项2的变体)。尽管如此,它带来了一个额外的部署难题
- 编写一个SQLUDF来解析层次结构字符串,并将其放在DB层上。这里最大的问题似乎是缺少数组处理/regex
- 在数据库层上按hierarchyID排序,并使用ROW_NUMBER()函数作为排序顺序的替代
- 在.net层上编写一些helper方法,重新解析hierarchyId.ToString()并生成可排序字符串(即“/0003/0004/0012/”)
所以我的问题是有没有人绕过这个限制?您是否使用了上述任何策略?如果是,怎么做?嗯,我似乎收到了一些意见,但没有得到任何回应。我迫切需要处理SQL上面的层次结构,所以我创建了一个静态助手类。我不认为这是一个完整的解决方案,但到目前为止,它工作得比较好。code>PadPath确实是这里的关键功能
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) + "/";
}
}
公共静态类sqlhierarchyin{
const int DEFAULT_PAD_LEN=3;
const char DEFAULT_PAD_char='0';
公共静态字符串PadPath(字符串层次结构){
返回焊盘路径(层次结构,默认焊盘长度);
}
公共静态字符串PadPath(字符串层次结构,int-padLen){
string[]components=Hierarchy.Split('/');
对于(var i=0;i
希望这对别人有帮助!尽管如此,我还是想听听人们的意见