From 2a28986c8de6b3796b21a928fdb4b167b699577a Mon Sep 17 00:00:00 2001 From: Iosebyte <4988743+iosebyte@users.noreply.github.com> Date: Wed, 14 Aug 2019 22:47:02 +0200 Subject: [PATCH] Fixes System.ArgumentException when GetAncestor returns the root node (#1125) --- src/EntityFramework/Hierarchy/HierarchyId.cs | 4 ++++ .../UnitTests/Hierarchy/HierarchyIdUnitTests.cs | 1 + 2 files changed, 5 insertions(+) diff --git a/src/EntityFramework/Hierarchy/HierarchyId.cs b/src/EntityFramework/Hierarchy/HierarchyId.cs index 02b84d2ee2..f42b9d8fb9 100644 --- a/src/EntityFramework/Hierarchy/HierarchyId.cs +++ b/src/EntityFramework/Hierarchy/HierarchyId.cs @@ -97,6 +97,10 @@ public HierarchyId GetAncestor(int n) { return new HierarchyId(null); } + else if (GetLevel() == n) + { + return new HierarchyId(PathSeparator); + } string hierarchyStr = PathSeparator + string.Join(PathSeparator, _nodes.Take(GetLevel() - n).Select(IntArrayToStirng)) + PathSeparator; diff --git a/test/EntityFramework/UnitTests/Hierarchy/HierarchyIdUnitTests.cs b/test/EntityFramework/UnitTests/Hierarchy/HierarchyIdUnitTests.cs index 41a30246d2..bfce17aa27 100644 --- a/test/EntityFramework/UnitTests/Hierarchy/HierarchyIdUnitTests.cs +++ b/test/EntityFramework/UnitTests/Hierarchy/HierarchyIdUnitTests.cs @@ -21,6 +21,7 @@ public void Check_hierarchyid_functions() Assert.Equal(new HierarchyId("/1/2.1/3/").GetAncestor(2), new HierarchyId("/1/")); Assert.Equal(new HierarchyId("/1/").GetAncestor(2) == null, true); Assert.Equal(new HierarchyId("/1/").GetAncestor(2) == new HierarchyId(null), true); + Assert.Equal(new HierarchyId("/1/").GetAncestor(1), new HierarchyId("/")); Assert.Equal(new HierarchyId("/1/").IsDescendantOf(null), true); Assert.Equal(new HierarchyId("/1/").IsDescendantOf(new HierarchyId(null)), true); Assert.Equal(new HierarchyId("/1/2/").IsDescendantOf(new HierarchyId("/")), true);