0

I have an object like below:

roleNode = //child hierarchyid,
code = model.code,
name = model.name,
created = now,
createdById = Convert.ToInt32(HttpContext.User.FindFirst(CrmClaimTypes.UserId).Value),
updated = now,
updatedById = Convert.ToInt32(HttpContext.User.FindFirst(CrmClaimTypes.UserId).Value),
IsActive = true

My parameters:

{
    "parentRoleNode": "1/1/2"
} 

How can I create a childNode from parentNode using Entity Framework?

  • Note that both "1/1/2" and "1/1/2/1" is available down below at SQL Server Database.
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47

1 Answers1

0

You need to check all your nodes until you find the empty node

Create new view model for your entity and change the type object with your model name

    public class RoleTreeView
    {
        public object Parent { get; set; }
        public object Child { get; set; }

        public RoleTreeView()
        {
        }

        public RoleTreeView(object parent)
        {
            CreateRoleTreeView(parent);
        }

        public RoleTreeView CreateRoleTreeView(object Parent)
        {
            var model = new RoleTreeView();
            if (Parent.roleNode != null)
            {
                model.Parent = Parent;
                model.Child = //get from db base on id; 
                CreateRoleTreeView(model.Child);
            }
            return model;
       }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shervin Ivari
  • 1,759
  • 5
  • 20
  • 28
  • Thanks you for your answer. I just don't know the way to search for all childrenNode of that parentNode. Ex: • Parent: "1/1/2" • Current Chidren: "1/1/2/1" • Child need to be added: "1/1/2/2" • I now think about the way converting to String and then mange by some methods just like parse string..... :( – Whiskey Truong Dec 11 '20 at 09:59
  • @VũThiên each Parents Have only one children? – Shervin Ivari Dec 11 '20 at 10:01
  • @VũThiên You need to create new object var vm = new RoleTreeView(firstParent); The CreateRoleTreeView function check all models base on if they have any child id – Shervin Ivari Dec 11 '20 at 10:03
  • @VũThiên Your welcome,Just make sure you have correct child hierarchyid – Shervin Ivari Dec 11 '20 at 10:06