示例#1
0
        /// <summary>
        /// Inserts edges into the database
        /// </summary>
        /// <param name="entries">The entries to add to the database</param>
        public string AddGraph(IList <Entry> entries)
        {
            if (entries == null)
            {
                throw new ArgumentException("entries cannot be null");
            }

            using (var context = new MyDbDataContext()) {
                Graph graph = new Graph();
                context.Graph.Add(graph);
                context.SaveChanges();

                for (int i = 0; i < entries.Count; i += BUFFER_SIZE)
                {
                    var buffer = entries.Skip(BUFFER_SIZE * i).Take(BUFFER_SIZE + i * BUFFER_SIZE);

                    var names = buffer.Select(x => x.ParentName);
                    names = names.Union(buffer.Select(x => x.ChildName));
                    names = names.ToList();

                    // Prefetch less than 2 * BUFFER_SIZE node Ids for inserting once per BUFFER_SIZE entries
                    var nodes = context.Node.Where(x => names.Contains(x.Name)).
                                Select(d => new { d.Name, d.Id }).
                                ToDictionary(d => d.Name, d => d.Id);
                    foreach (var e in buffer)
                    {
                        Edge edge = new Edge();
                        edge.ParentNodeId = nodes[e.ParentName];
                        edge.ChildNodeId  = nodes[e.ChildName];
                        edge.Quantity     = e.Quantity;
                        edge.GraphId      = graph.Id;
                        context.Set <Edge>().Add(edge);
                    }
                    context.SaveChanges();
                    return(graph.Guid.ToString());
                }
            }
            return(null);
        }