public override void CreateViewModel(EditingContext ctx)
        {
            var service = ctx.GetEFArtifactService();
            Debug.Assert(service != null, "Null service in ExplorerViewModelHelper.CreateViewModel()");
            var artifact = service.Artifact;
            Debug.Assert(artifact != null, "Null artifact in ExplorerViewModelHelper.CreateViewModel()");

            var xref = ModelToExplorerModelXRef.GetModelToBrowserModelXRef(ctx);
            xref.Clear();

            var edmRootNode = new ExplorerRootNode(ctx, null, artifact.Uri);

            var designerInfo = artifact.DesignerInfo();
            if (designerInfo != null
                && designerInfo.Diagrams != null)
            {
                var explorerDiagrams = (ExplorerDiagrams)
                                       ModelToExplorerModelXRef.GetNewOrExisting(
                                           ctx, designerInfo.Diagrams, edmRootNode, typeof(ExplorerDiagrams));
                edmRootNode.Diagrams = explorerDiagrams;
            }

            if (artifact.ConceptualModel() != null)
            {
                var browserCsdlEntityModel = (ExplorerConceptualEntityModel)
                                             ModelToExplorerModelXRef.GetNewOrExisting(
                                                 ctx, artifact.ConceptualModel(), edmRootNode, typeof(ExplorerConceptualEntityModel));
                edmRootNode.ConceptualModel = browserCsdlEntityModel;
            }

            if (artifact.StorageModel() != null)
            {
                var browserSsdlEntityModel = (ExplorerStorageEntityModel)
                                             ModelToExplorerModelXRef.GetNewOrExisting(
                                                 ctx, artifact.StorageModel(), edmRootNode, typeof(ExplorerStorageEntityModel));
                edmRootNode.StorageModel = browserSsdlEntityModel;
            }

            // expand the tree view so that the Conceptual, Storage Models, and Diagram nodes are visible
            if (edmRootNode.Diagrams != null)
            {
                edmRootNode.Diagrams.Types.ExpandTreeViewToMe();
            }

            if (edmRootNode.ConceptualModel != null)
            {
                edmRootNode.ConceptualModel.Types.ExpandTreeViewToMe();
            }

            if (edmRootNode.StorageModel != null)
            {
                edmRootNode.StorageModel.Types.ExpandTreeViewToMe();
            }

            base.ViewModel = new ExplorerViewModel(ctx, edmRootNode);
        }
 internal static EFArtifact GetArtifact(EditingContext context)
 {
     if (context != null)
     {
         var service = context.GetEFArtifactService();
         if (service != null)
         {
             return service.Artifact;
         }
     }
     return null;
 }
        internal PropertyExtensionContextImpl(
            EditingContext editingContext, ProjectItem projectItem, Version targetSchemaVersion, byte[] extensionToken)
        {
            Debug.Assert(editingContext != null, "editingContext should not be null");
            Debug.Assert(editingContext.GetEFArtifactService().Artifact != null, "editingContext should not have null artifact");
            Debug.Assert(projectItem != null, "projectItem should not be null");
            Debug.Assert(extensionToken != null, "extensionToken should not be null");

            _editingContext = editingContext;
            _projectItem = projectItem;
            _targetSchemaVersion = targetSchemaVersion;
            _extensionToken = extensionToken;
        }
        internal static ModelToExplorerModelXRef GetModelToBrowserModelXRef(EditingContext context)
        {
            Type modelToExplorerModelXRefType;
            if (
                !_modelManagerType2XRefType.TryGetValue(
                    context.GetEFArtifactService().Artifact.ModelManager.GetType(), out modelToExplorerModelXRefType))
            {
                Debug.Fail(
                    "Could not find a ModelToExplorerModelXRef type for the ModelManager of type '"
                    + context.GetEFArtifactService().Artifact.ModelManager.GetType()
                    + "'.  Make sure to call AddModelManager2XRefType before calling GetModelToBrowserModelXRef.");
                return null;
            }

            // Update EFElement to BrowserEFElement cross reference so that Search Results can later access it
            var xref = (ModelToExplorerModelXRef)context.Items.GetValue(modelToExplorerModelXRefType);
            if (xref == null)
            {
                xref = (ModelToExplorerModelXRef)Activator.CreateInstance(modelToExplorerModelXRefType);
                context.Items.SetValue(xref);
            }
            return xref;
        }
        /// <summary>
        ///     Returns the current active artifacts.
        ///     Assumption in here is that a view can span multiple artifacts..
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected virtual HashSet<EFArtifact> GetCurrentArtifactsInView(EditingContext context)
        {
            var serviceFromContext = context.GetEFArtifactService();
            Debug.Assert(
                serviceFromContext != null, "Null service in EditingContext for ExplorerViewModelHelper.ProcessModelChangesCommitted()");

            var artifacts = new HashSet<EFArtifact>();
            artifacts.Add(serviceFromContext.Artifact);

            return artifacts;
        }
        protected override HashSet<EFArtifact> GetCurrentArtifactsInView(EditingContext context)
        {
            var artifacts = new HashSet<EFArtifact>();
            var service = context.GetEFArtifactService();
            var entityDesignArtifact = service.Artifact as EntityDesignArtifact;

            Debug.Assert(
                entityDesignArtifact != null,
                "The active artifact is not type of: " + typeof(EntityDesignArtifact) + ", Actual:" + service.Artifact.GetType());
            if (entityDesignArtifact != null)
            {
                artifacts.Add(entityDesignArtifact);

                if (entityDesignArtifact.DiagramArtifact != null)
                {
                    artifacts.Add(entityDesignArtifact.DiagramArtifact);
                }
            }
            return artifacts;
        }
        private static MicrosoftDataEntityDesignDocData GetDocData(EditingContext editingContext)
        {
            Debug.Assert(editingContext != null, "editingContext != null");

            var artifactService = editingContext.GetEFArtifactService();
            return (MicrosoftDataEntityDesignDocData)VSHelpers.GetDocData(ServiceProvider, artifactService.Artifact.Uri.LocalPath);
        }
        internal static void CreateDefaultDiagram(EditingContext context, EntityDesignerDiagram diagram)
        {
            var service = context.GetEFArtifactService();
            var artifact = service.Artifact;
            Debug.Assert(artifact != null, "Artifact is null");

            var cpc = new CommandProcessorContext(
                context, EfiTransactionOriginator.EntityDesignerOriginatorId, EntityDesignerResources.Tx_CreateDiagram);
            var cmd = new DelegateCommand(
                () =>
                    {
                        EntityDesignerDiagramAdd.StaticInvoke(cpc, diagram);

                        foreach (var shapeElement in diagram.NestedChildShapes)
                        {
                            var entityShape = shapeElement as EntityTypeShape;
                            if (entityShape != null)
                            {
                                EntityTypeShapeAdd.StaticInvoke(cpc, entityShape);
                                EntityTypeShapeChange.StaticInvoke(
                                    cpc, entityShape, ViewModelDiagram.NodeShape.AbsoluteBoundsDomainPropertyId);
                                EntityTypeShapeChange.StaticInvoke(cpc, entityShape, ViewModelDiagram.NodeShape.IsExpandedDomainPropertyId);
                                continue;
                            }

                            var associationConnector = shapeElement as AssociationConnector;
                            if (associationConnector != null)
                            {
                                AssociationConnectorAdd.StaticInvoke(cpc, associationConnector);
                                AssociationConnectorChange.StaticInvoke(
                                    cpc, associationConnector, ViewModelDiagram.LinkShape.EdgePointsDomainPropertyId);
                                AssociationConnectorChange.StaticInvoke(
                                    cpc, associationConnector, ViewModelDiagram.LinkShape.ManuallyRoutedDomainPropertyId);
                                continue;
                            }

                            var inheritanceConnector = shapeElement as InheritanceConnector;
                            if (inheritanceConnector != null)
                            {
                                InheritanceConnectorAdd.StaticInvoke(cpc, inheritanceConnector);
                                InheritanceConnectorChange.StaticInvoke(
                                    cpc, inheritanceConnector, ViewModelDiagram.LinkShape.EdgePointsDomainPropertyId);
                                InheritanceConnectorChange.StaticInvoke(
                                    cpc, inheritanceConnector, ViewModelDiagram.LinkShape.ManuallyRoutedDomainPropertyId);
                                continue;
                            }
                        }
                    });
            CommandProcessor.InvokeSingleCommand(cpc, cmd);
        }
        // <summary>
        //     Show the dialog to create a new enum type.
        // </summary>
        public static EnumType AddNewEnumType(
            string selectedUnderlyingType, EditingContext editingContext, string originatingId, EventHandler onDialogActivated = null)
        {
            if (editingContext == null)
            {
                throw new ArgumentNullException("editingContext");
            }

            var artifactService = editingContext.GetEFArtifactService();
            var entityDesignArtifact = artifactService.Artifact as EntityDesignArtifact;

            Debug.Assert(
                entityDesignArtifact != null,
                typeof(EntityDesignViewModelHelper).Name
                + ".AddEnumType: Unable to find Entity Design Artifact from the passed in editing context.");

            if (entityDesignArtifact != null)
            {
                var vm = new EnumTypeViewModel(entityDesignArtifact, selectedUnderlyingType);

                var result = ShowEnumTypeDialog(vm, onDialogActivated);

                if (result == true
                    && vm.IsValid)
                {
                    var cp = new CommandProcessor(editingContext, originatingId, Resources.Tx_CreateEnumType);
                    var createEnumTypeCommand = new CreateEnumTypeCommand(
                        vm.Name, vm.SelectedUnderlyingType
                        , (vm.IsReferenceExternalType ? vm.ExternalTypeName : String.Empty), vm.IsFlag, false);

                    cp.EnqueueCommand(createEnumTypeCommand);

                    foreach (var member in vm.Members)
                    {
                        if (String.IsNullOrWhiteSpace(member.Name) == false)
                        {
                            cp.EnqueueCommand(new CreateEnumTypeMemberCommand(createEnumTypeCommand, member.Name, member.Value));
                        }
                    }
                    cp.Invoke();
                    return createEnumTypeCommand.EnumType;
                }
            }
            return null;
        }