public Block AppendSubservice(string ns)
        {
            Block systemRoot = context.Blocks.SingleOrDefault(x => x.Path == HierarchyId.GetRoot());
            if (systemRoot == null)
            {
                systemRoot = AddRoot();
            }

            var subservice = new Block { Id = Guid.NewGuid(), BlockName = ns, Path = GetAppendPath(systemRoot.Path) };
            context.Blocks.Add(subservice);
            context.SaveChanges();
            return subservice;
        }
 public IEnumerable<Block> GetChildren(Block parent)
 {
     return context.Blocks.Where(
         x => x.Path.IsDescendantOf(parent.Path) && x.Path.GetLevel() == parent.Path.GetLevel() + 1);
 }
 private TreeItem Map(Block blockToMap)
 {
     var newItem = new TreeItem()
     {
         Name = blockToMap.BlockName,
         IsChoice = blockToMap.IsChoice,
         Order = blockToMap.Order,
         SubItems = new List<TreeItem>()
     };
     newItem.SubItems.AddRange(blockToMap.AttributeList.Select(x => new TreeItem
         {
             Name = x.AttributeName,
             Order = x.Order,
             Placeholder = x.Placeholder
         }));
     foreach (Block block in GetChildren(blockToMap))
     {
         newItem.SubItems.Add(Map(block));
     }
     newItem.SubItems.Sort(new TreeNodeComparer());
     return newItem;
 }
 public Block AddRoot()
 {
     Block systemRoot = new Block() { Id = Guid.NewGuid(), BlockName = "_SystemRoot", Path = HierarchyId.GetRoot() };
     context.Blocks.Add(systemRoot);
     return systemRoot;
 }
        private void Dive(TreeItem currentNode, HierarchyId currentPath)
        {
            var newBlock = new Block
            {
                Id = Guid.NewGuid(),
                Path = currentPath,
                BlockName = currentNode.Name,
                IsChoice = currentNode.IsChoice,
                Order = currentNode.Order,
                AttributeList = new Collection<AttributeMetadata>()
            };
            var subBlocks = currentNode.SubItems.Where(x => x.SubItems != null).ToArray();

            //блок
            for (int i = 1; i <= subBlocks.Length; i++)
            {
                var subItem = currentNode.SubItems[i - 1];
                Dive(subItem, HierarchyId.Parse(currentPath + i.ToString() + "/"));
            }
            /*var subAttributes = currentNode.SubItems.Where(x => x.SubItems == null).Select(x => new AttributeMetadata
                {
                    Id = Guid.NewGuid(),
                    AttributeName = x.Name,
                    BlockId = newBlock.Id,
                    Placeholder = x.Placeholder,
                });*/
            foreach (var node in currentNode.SubItems.Where(x => x.SubItems == null))
            {
                newBlock.AttributeList.Add(new AttributeMetadata
                {
                    Id = Guid.NewGuid(),
                    AttributeName = node.Name,
                    Placeholder = node.Placeholder,
                    Order = node.Order
                });
            }
            context.Blocks.Add(newBlock);
        }