public IEnumerable<node> GetAllNodes() { List<node> result = new List<node>(); using (var context = new GraphContext()) { foreach (var n in context.Nodes) { var nreturn = new node() { id = n.Id, label = n.Name, adjacentNodes = new node.adjacentNodesType() }; nreturn.adjacentNodes.AddRange(n.Paths.Select(a => a.DestinationNode.Id)); result.Add(nreturn); } } return result; }
public void UpdateNodes(IEnumerable<node> wiredNodes) { List<Node> nodes = wiredNodes.Select(n => new Node() { Id = n.id, Name = n.label }).ToList(); foreach (var node in nodes) { foreach (var neighbour in wiredNodes.First(n => n.id == node.Id).adjacentNodes) node.Paths.Add(new Path() { DestinationNode = nodes.First(n => n.Id == neighbour), OriginNode = node }); } using (var context = new GraphContext()) { var existingNodes = context.Nodes; foreach (var path in context.Paths) { context.Paths.Remove(path); } foreach (var node in existingNodes) { context.Nodes.Remove(node); } context.SaveChanges(); foreach (var node in nodes) { context.Nodes.Add(node); } context.SaveChanges(); } logger.InfoFormat("Updated database with provided nodes."); }