0

The suggested SQL code by Microsoft to move a HierarchyId node is:

CREATE PROCEDURE MoveOrg(@oldMgr nvarchar(256), @newMgr nvarchar(256) )  
AS  
BEGIN  
DECLARE @nold hierarchyid, @nnew hierarchyid  
SELECT @nold = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @oldMgr ;  
  
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE  
BEGIN TRANSACTION  
SELECT @nnew = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @newMgr ;  
  
SELECT @nnew = @nnew.GetDescendant(max(OrgNode), NULL)   
FROM HumanResources.EmployeeDemo WHERE OrgNode.GetAncestor(1)=@nnew ;  
  
UPDATE HumanResources.EmployeeDemo    
SET OrgNode = OrgNode.GetReparentedValue(@nold, @nnew)  
WHERE OrgNode.IsDescendantOf(@nold) = 1 ;  
  
COMMIT TRANSACTION;
END ;  
GO

I know I can use the stored procedure directly with EF Core, but prefer to have it as a normal C# code transaction.

My general question is how would I do the selects within the transaction to update values with EF Core? Or even better, what would be the equivalent code in C#?

Aktaeon
  • 189
  • 2
  • 14
  • [Wait for EF 8.0](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#sql-server-hierarchyid). – Gert Arnold Aug 23 '23 at 18:23
  • Thanks Gert, while it will certainly help rewriting the nodes, and I do like the way the sets work, but looking at the code the query to get the new root is not within the transactional boundary. – Aktaeon Aug 23 '23 at 19:27

0 Answers0