public override bool RenameXmlModel(Uri oldName, Uri newName)
        {
            VSXmlModel vsXmlModel = null;

            if (_xmlModels.TryGetValue(oldName, out vsXmlModel))
            {
                _xmlModels.Remove(oldName);
                _xmlModels.Add(newName, vsXmlModel);
                Debug.Assert(new Uri(vsXmlModel.Name) == newName);
                return(true);
            }
            return(false);
        }
        public override void CloseXmlModel(Uri xmlModelUri)
        {
            VSXmlModel vsXmlModel = null;

            // Note: _xmlModels can be null if we have already been disposed
            if (_xmlModels != null &&
                _xmlModels.TryGetValue(xmlModelUri, out vsXmlModel))
            {
                _xmlModels.Remove(xmlModelUri);
                vsXmlModel.Dispose();
            }
            base.CloseXmlModel(xmlModelUri);
        }
        public override XmlModel GetXmlModel(Uri sourceUri)
        {
            Debug.Assert(_xmlDesignerPackage.IsForegroundThread, "Can't request an XmlModel on background thread");

            // assert that an entry already exists in the RDT for this document and confirm with the designer package that the entry
            // is owned by the designer.  If this is not the case, the xml editor will create a doc data, and this may not be the doc
            // data we want.  This can result in failure to open the document in the desired designer because the editor is incorrect.
#if DEBUG
            var skipChecks = false;

#if VS12
            // The behaviour of VsShellUtilities.IsDocumentOpen changed in VS2013. In VS2012 IsDocumentOpen would return true if the document
            // has been loaded even though loading the solution has not finished yet. In VS2013 IsOpenDocument returns false if the solution
            // is still being loaded. This caused multiple asserts when opening a project after VS was closed when edmx file was active/opened
            // See http://entityframework.codeplex.com/workitem/1163 for more details and repro steps.
            var    solution = (IVsSolution)_services.GetService(typeof(IVsSolution));
            object propertyValue;
            if (solution != null && NativeMethods.Succeeded(solution.GetProperty((int)__VSPROPID2.VSPROPID_IsSolutionOpeningDocs, out propertyValue)))
            {
                skipChecks = true;
            }
#endif

            // Alert: when we try to load the XmlModel for diagram file, the document is not opened in VS.
            //  The If statement is added to skip the check for diagram files.
            if (!skipChecks &&
                sourceUri.LocalPath.EndsWith(".edmx", StringComparison.OrdinalIgnoreCase))
            {
                IVsUIHierarchy hier;
                uint           itemId;
                IVsWindowFrame windowFrame;
                var            isDocumentOpen = VsShellUtilities.IsDocumentOpen(
                    _services, sourceUri.LocalPath, Guid.Empty, out hier, out itemId, out windowFrame);

                Debug.Assert(isDocumentOpen, "Running Document Table does not contain document in GetXmlModel()");
                if (isDocumentOpen)
                {
                    var frameWrapper = _xmlDesignerPackage.DocumentFrameMgr.CreateFrameWrapper(windowFrame);
                    Debug.Assert(frameWrapper != null, "Could not construct FrameWrapper for IVsWindowFrame in debug code in GetXmlModel()");
                    if (frameWrapper != null)
                    {
                        Debug.Assert(
                            frameWrapper.IsDesignerDocInDesigner,
                            "We are trying to GetXmlModel() for a document that is not owned by the designer");
                    }
                }
            }
#endif

            VSXmlModel vsXmlModel = null;
            if (!_xmlModels.TryGetValue(sourceUri, out vsXmlModel))
            {
                Microsoft.VisualStudio.XmlEditor.XmlModel xmlModel = null;
                try
                {
                    xmlModel = _xmlStore.OpenXmlModel(sourceUri);
                }
                catch (Exception)
                {
                    xmlModel = null;
                }
                if (xmlModel != null)
                {
                    vsXmlModel = new VSXmlModel(_services, xmlModel);
                    _xmlModels.Add(sourceUri, vsXmlModel);
                }
            }

            return(vsXmlModel);
        }
        public override XmlModel GetXmlModel(Uri sourceUri)
        {
            Debug.Assert(_xmlDesignerPackage.IsForegroundThread, "Can't request an XmlModel on background thread");

            // assert that an entry already exists in the RDT for this document and confirm with the designer package that the entry
            // is owned by the designer.  If this is not the case, the xml editor will create a doc data, and this may not be the doc 
            // data we want.  This can result in failure to open the document in the desired designer because the editor is incorrect.
#if DEBUG
            var skipChecks = false;

#if VS12ORNEWER
    // The behaviour of VsShellUtilities.IsDocumentOpen changed in VS2013. In VS2012 IsDocumentOpen would return true if the document
    // has been loaded even though loading the solution has not finished yet. In VS2013 IsOpenDocument returns false if the solution 
    // is still being loaded. This caused multiple asserts when opening a project after VS was closed when edmx file was active/opened
    // See http://entityframework.codeplex.com/workitem/1163 for more details and repro steps.
            var solution = (IVsSolution)_services.GetService(typeof (IVsSolution));
            object propertyValue;
            if(solution != null && NativeMethods.Succeeded(solution.GetProperty((int)__VSPROPID2.VSPROPID_IsSolutionOpeningDocs, out propertyValue)))
            {
                skipChecks = true;
            }
#endif

            // Alert: when we try to load the XmlModel for diagram file, the document is not opened in VS.
            //  The If statement is added to skip the check for diagram files.
            if (!skipChecks
                && sourceUri.LocalPath.EndsWith(".edmx", StringComparison.OrdinalIgnoreCase))
            {
                IVsUIHierarchy hier;
                uint itemId;
                IVsWindowFrame windowFrame;
                var isDocumentOpen = VsShellUtilities.IsDocumentOpen(
                    _services, sourceUri.LocalPath, Guid.Empty, out hier, out itemId, out windowFrame);

                Debug.Assert(isDocumentOpen, "Running Document Table does not contain document in GetXmlModel()");
                if (isDocumentOpen)
                {
                    var frameWrapper = _xmlDesignerPackage.DocumentFrameMgr.CreateFrameWrapper(windowFrame);
                    Debug.Assert(frameWrapper != null, "Could not construct FrameWrapper for IVsWindowFrame in debug code in GetXmlModel()");
                    if (frameWrapper != null)
                    {
                        Debug.Assert(
                            frameWrapper.IsDesignerDocInDesigner,
                            "We are trying to GetXmlModel() for a document that is not owned by the designer");
                    }
                }
            }
#endif

            VSXmlModel vsXmlModel = null;
            if (!_xmlModels.TryGetValue(sourceUri, out vsXmlModel))
            {
                Microsoft.VisualStudio.XmlEditor.XmlModel xmlModel = null;
                try
                {
                    xmlModel = _xmlStore.OpenXmlModel(sourceUri);
                }
                catch (Exception)
                {
                    xmlModel = null;
                }
                if (xmlModel != null)
                {
                    vsXmlModel = new VSXmlModel(_services, xmlModel);
                    _xmlModels.Add(sourceUri, vsXmlModel);
                }
            }

            return vsXmlModel;
        }