Exemplo n.º 1
0
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // First check if the diagram file exists, if yes quit immediately.
            var diagramFilePath = _artifact.Uri.LocalPath + EntityDesignArtifact.ExtensionDiagram;

            if (!File.Exists(diagramFilePath) &&
                (_artifact != null))
            {
                // The diagram file should have same structure as the EDMX file but only contains diagrams node.
                // To accomplish that the method will the do the following:
                // - Load the EDMX file to XDocument.
                // - Store Diagrams root node.
                // - Remove all nodes under root node.
                // - Create and add Designer node under root node.
                // - Re-Add Diagrams node under Designer node.
                var document         = XDocument.Parse(_artifact.XDocument.ToString(), LoadOptions.PreserveWhitespace);
                var rootDiagramsNode =
                    document.Descendants(XName.Get("Diagrams", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)))
                    .FirstOrDefault();
                Debug.Assert(rootDiagramsNode != null, "Diagrams node does not exist in the EDMX file.");

                if (rootDiagramsNode != null)
                {
                    // Remove any nodes under root.
                    document.Root.RemoveNodes();
                    // Create an empty Designer node.
                    var element2 = new XElement(XName.Get("Designer", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)));
                    element2.Add(rootDiagramsNode);
                    document.Root.Add(element2);
                    // Save the diagram file to disk.
                    document.Save(diagramFilePath, SaveOptions.None);

                    // Remove all diagram nodes under Diagrams node.
                    // Note that we want to still keep the Diagrams node for backward compatibility purpose. (The EDMX file could still be opened in the older Visual Studio).
                    _artifact.XDocument.Descendants(XName.Get("Diagrams", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)))
                    .First()
                    .RemoveNodes();

                    // The code below is to ensure that Diagrams artifact in instantiated and initialized properly.
                    DiagramArtifact efArtifact = new VSDiagramArtifact(
                        _artifact.ModelManager, new Uri(diagramFilePath), _artifact.XmlModelProvider);
                    _artifact.DiagramArtifact = efArtifact;
                    _artifact.ModelManager.RegisterArtifact(efArtifact, _artifact.ArtifactSet);
                }
            }
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // First check if the diagram file exists, if yes quit immediately.
            var diagramFilePath = _artifact.Uri.LocalPath + EntityDesignArtifact.ExtensionDiagram;
            if (!File.Exists(diagramFilePath)
                && (_artifact != null))
            {
                // The diagram file should have same structure as the EDMX file but only contains diagrams node.
                // To accomplish that the method will the do the following:
                // - Load the EDMX file to XDocument.
                // - Store Diagrams root node.
                // - Remove all nodes under root node.
                // - Create and add Designer node under root node.
                // - Re-Add Diagrams node under Designer node.
                var document = XDocument.Parse(_artifact.XDocument.ToString(), LoadOptions.PreserveWhitespace);
                var rootDiagramsNode =
                    document.Descendants(XName.Get("Diagrams", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)))
                        .FirstOrDefault();
                Debug.Assert(rootDiagramsNode != null, "Diagrams node does not exist in the EDMX file.");

                if (rootDiagramsNode != null)
                {
                    // Remove any nodes under root.
                    document.Root.RemoveNodes();
                    // Create an empty Designer node.
                    var element2 = new XElement(XName.Get("Designer", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)));
                    element2.Add(rootDiagramsNode);
                    document.Root.Add(element2);
                    // Save the diagram file to disk.
                    document.Save(diagramFilePath, SaveOptions.None);

                    // Remove all diagram nodes under Diagrams node.
                    // Note that we want to still keep the Diagrams node for backward compatibility purpose. (The EDMX file could still be opened in the older Visual Studio).
                    _artifact.XDocument.Descendants(XName.Get("Diagrams", SchemaManager.GetEDMXNamespaceName(_artifact.SchemaVersion)))
                        .First()
                        .RemoveNodes();

                    // The code below is to ensure that Diagrams artifact in instantiated and initialized properly.
                    DiagramArtifact efArtifact = new VSDiagramArtifact(
                        _artifact.ModelManager, new Uri(diagramFilePath), _artifact.XmlModelProvider);
                    _artifact.DiagramArtifact = efArtifact;
                    _artifact.ModelManager.RegisterArtifact(efArtifact, _artifact.ArtifactSet);
                }
            }
        }