public void SaveDifferences(int assessment_id) { ///when saving if the parent object is a layer then there is not default zone ///if the parent object is zone then all objects in that zone inherit the layer of the zone /* * remove all deleted zones and layers * remove * add in all the layers and zones * add all the components for each layer and zone * */ DiagramDifferences differences = new DiagramDifferences(); differences.processComparison(newDiagram, oldDiagram); foreach (var deleteNode in differences.DeletedNodes) { var adc = context.ASSESSMENT_DIAGRAM_COMPONENTS .FirstOrDefault(x => x.Assessment_Id == assessment_id && x.Component_Guid == deleteNode.Key); if (adc != null) { context.ASSESSMENT_DIAGRAM_COMPONENTS.Remove(adc); } } foreach (var layer in differences.DeletedLayers) { var adc = context.DIAGRAM_CONTAINER.FirstOrDefault(x => x.Assessment_Id == assessment_id && x.DrawIO_id == layer.Key); if (adc != null) { context.DIAGRAM_CONTAINER.Remove(adc); } } foreach (var zone in differences.DeletedZones) { var adc = context.DIAGRAM_CONTAINER.FirstOrDefault(x => x.Assessment_Id == assessment_id && x.DrawIO_id == zone.Key); if (adc != null) { context.DIAGRAM_CONTAINER.Remove(adc); } } foreach (var layer in differences.AddedContainers) { var l = context.DIAGRAM_CONTAINER.Where(x => x.Assessment_Id == assessment_id && x.DrawIO_id == layer.Key).FirstOrDefault(); if (l == null) { context.DIAGRAM_CONTAINER.Add(new DIAGRAM_CONTAINER() { Assessment_Id = assessment_id, ContainerType = CONTAINER_TYPE_LAYER, DrawIO_id = layer.Key, Parent_Draw_IO_Id = layer.Value.Parent_id, Name = layer.Value.LayerName, Visible = layer.Value.Visible }); } else { l.Name = layer.Value.LayerName; l.Visible = layer.Value.Visible; } } //case were the only change was a layer visibility change foreach (var layer in newDiagram.Layers) { var l = context.DIAGRAM_CONTAINER.Where(x => x.Assessment_Id == assessment_id && x.DrawIO_id == layer.Key).FirstOrDefault(); if (l != null) { l.Name = layer.Value.LayerName; l.Visible = layer.Value.Visible; } } context.SaveChanges(); Dictionary <string, int> layerLookup = context.DIAGRAM_CONTAINER.Where(x => x.Assessment_Id == assessment_id).ToList().ToDictionary(x => x.DrawIO_id, x => x.Container_Id); int defaultLayer; //if we didn't find the default layer then just add it. //no sure why we wouldn't but some how that is the case. if (!layerLookup.TryGetValue("1", out defaultLayer)) { var layer = new DIAGRAM_CONTAINER() { Assessment_Id = assessment_id, ContainerType = CONTAINER_TYPE_LAYER, DrawIO_id = "1", Name = Constants.DEFAULT_LAYER_NAME, Parent_Id = 0, Parent_Draw_IO_Id = null, Universal_Sal_Level = "L", Visible = true }; context.DIAGRAM_CONTAINER.Add(layer); defaultLayer = layer.Container_Id; } foreach (var zone in newDiagram.Zones) { var z = context.DIAGRAM_CONTAINER.Where(x => x.Assessment_Id == assessment_id && x.DrawIO_id == zone.Key).FirstOrDefault(); if (z == null) { if (!layerLookup.TryGetValue(zone.Value.Parent_id, out int parent_id)) { parent_id = defaultLayer; } z = new DIAGRAM_CONTAINER() { Assessment_Id = assessment_id, ContainerType = "Zone", DrawIO_id = zone.Key, Name = zone.Value.ComponentName, Universal_Sal_Level = zone.Value.SAL, Parent_Id = parent_id, Parent_Draw_IO_Id = zone.Value.Parent_id }; context.DIAGRAM_CONTAINER.Add(z); } else { z.Universal_Sal_Level = zone.Value.SAL; z.Name = zone.Value.ComponentName; } } context.SaveChanges(); LayerManager layers = new LayerManager(context, assessment_id); Dictionary <Guid, ASSESSMENT_DIAGRAM_COMPONENTS> adcDictionary = context.ASSESSMENT_DIAGRAM_COMPONENTS.Where(x => x.Assessment_Id == assessment_id).ToDictionary(x => x.Component_Guid, x => x); foreach (var newNode in newDiagram.NetworkComponents) { ASSESSMENT_DIAGRAM_COMPONENTS adc; if (adcDictionary.TryGetValue(newNode.Key, out adc)) { adc.Assessment_Id = assessment_id; adc.Component_Guid = newNode.Key; adc.Component_Symbol_Id = newNode.Value.Component_Symbol_Id; adc.DrawIO_id = newNode.Value.ID; adc.Parent_DrawIO_Id = newNode.Value.Parent_id; adc.label = newNode.Value.ComponentName; } else { adc = new ASSESSMENT_DIAGRAM_COMPONENTS() { Assessment_Id = assessment_id, Component_Guid = newNode.Key, Component_Symbol_Id = newNode.Value.Component_Symbol_Id, DrawIO_id = newNode.Value.ID, Parent_DrawIO_Id = newNode.Value.Parent_id, label = newNode.Value.ComponentName, Layer_Id = null, Zone_Id = null }; context.ASSESSMENT_DIAGRAM_COMPONENTS.Add(adc); } } context.SaveChanges(); //tossing the whole approach and doing something different //save all containers, layers, and nodes //then go find and assign zone and layer var updateList = from a in context.ASSESSMENT_DIAGRAM_COMPONENTS join b in newDiagram.NetworkComponents on a.Component_Guid equals b.Key select new { a, b }; foreach (var pair in updateList.ToList()) { pair.a.Component_Symbol_Id = pair.b.Value.Component_Symbol_Id; pair.a.label = pair.b.Value.ComponentName; } context.SaveChanges(); layers.UpdateAllLayersAndZones(); context.FillNetworkDiagramQuestions(assessment_id); }