src/Misc/EFTools/EntityDesignEntityDesigner/CustomCode/DomainClasses/EntityDesignerViewModel.cs
/// <summary>
///     Deletes corresponding ModelItem (if exists in the view model)
/// </summary>
Deletes corresponding ModelItem (if exists in the view model)
Deletes corresponding ModelItem ( if exists in the view model )
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        private void OnEFObjectDeleted(EFObject efObject, ModelToDesignerModelXRefItem xref)
        {
            Debug.Assert(efObject != null);
            var key = efObject as Key;
            if (key != null
                && key.Parent != null)
            {
                var entityType = key.Parent as Model.Entity.EntityType;
                if (entityType != null)
                {
                    var viewEntityType = xref.GetExisting(entityType) as EntityType;
                    if (viewEntityType != null)
                    {
                        foreach (var property in entityType.Properties().Where(p => xref.GetExisting(p) != null))
                        {
                            ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                .SynchronizeSingleDslModelElement(viewEntityType, property);
                        }
                    }
                }
                return;
            }
            var propertyRef = efObject as PropertyRef;
            if (propertyRef != null
                && propertyRef.Parent is Key)
            {
                var property = propertyRef.Name.Target;
                if (property != null)
                {
                    var entityType = property.Parent as Model.Entity.EntityType;
                    Debug.Assert(entityType != null);
                    var viewEntityType = xref.GetExisting(entityType) as EntityType;
                    var viewProperty = xref.GetExisting(property) as Property;
                    if (viewEntityType != null
                        && viewProperty != null)
                    {
                        ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                            .SynchronizeSingleDslModelElement(viewEntityType, property);
                    }
                }
                return;
            }
            var modelProperty = efObject.Parent as ComplexConceptualProperty;
            if (modelProperty != null)
            {
                var entityType = modelProperty.Parent as Model.Entity.EntityType;
                if (entityType != null)
                {
                    var viewEntityType = xref.GetExisting(entityType) as EntityType;
                    if (viewEntityType != null)
                    {
                        Debug.Assert(xref.ContainsKey(modelProperty), "view for modelProperty not found");
                        ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                            .SynchronizeSingleDslModelElement(viewEntityType, modelProperty);
                    }
                }
                return;
            }
            ModelElement modelElement = null;
            var et = efObject as ConceptualEntityType;
            if (et != null)
            {
                foreach (var prop in et.Properties())
                {
                    modelElement = xref.GetExisting(prop);
                    if (modelElement != null)
                    {
                        ModelXRef.Remove(prop, modelElement);
                        modelElement.Delete();
                    }
                }
                foreach (var prop in et.NavigationProperties())
                {
                    modelElement = xref.GetExisting(prop);
                    if (modelElement != null)
                    {
                        ModelXRef.Remove(prop, modelElement);
                        modelElement.Delete();
                    }
                }
            }
            var modelDiagram = efObject as ModelDesigner.Diagram;
            var currentDiagram = GetDiagram();
            if (null != currentDiagram
                && null != modelDiagram
                && currentDiagram.DiagramId == modelDiagram.Id.Value)
            {
                var foundAnotherActiveDiagram = false;
                foreach (var diagram in Store.ElementDirectory.FindElements<EntityDesignerDiagram>())
                {
                    if (currentDiagram != diagram
                        && diagram.ActiveDiagramView != null
                        && diagram.IsDeleted == false
                        && diagram.IsDeleting == false)
                    {
                        foundAnotherActiveDiagram = true;
                        break;
                    }
                }
                if (!foundAnotherActiveDiagram)
                {
                    var service = EditingContext.GetEFArtifactService();
                    var entityDesignArtifact = service.Artifact as EntityDesignArtifact;
                    Debug.Assert(entityDesignArtifact != null, "EFArtifactService's artifact is null.");
                    if (entityDesignArtifact != null)
                    {
                        var firstModelDiagram = entityDesignArtifact.DesignerInfo.Diagrams.FirstDiagram;
                        Debug.Assert(
                            firstModelDiagram != null && firstModelDiagram != modelDiagram, "There is no valid diagram in the model.");
                        if (firstModelDiagram != null
                            && firstModelDiagram != modelDiagram)
                        {
                            currentDiagram.DiagramId = firstModelDiagram.Id.Value;
                            _shouldClearAndReloadDiagram = true;
                            return;
                        }
                    }
                }
            }
            modelElement = xref.GetExisting(efObject);
            if (modelElement != null)
            {
                ModelXRef.Remove(efObject, modelElement);
                modelElement.Delete();
            }
        }
[ SuppressMessage ( " " , " " ) ] [ SuppressMessage ( " " , " " ) ] private void OnEFObjectDeleted ( EFObject efObject , ModelToDesignerModelXRefItem xref ) { Debug . Assert ( efObject != null ) ; var key = efObject as Key ; if ( key != null && key . Parent != null ) { var entityType = key . Parent as Model . Entity . EntityType ; if ( entityType != null ) { var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; if ( viewEntityType != null ) { foreach ( var property in entityType . Properties ( ) . Where ( p => xref . GetExisting ( p ) != null ) ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , property ) ; } } } return ; } var propertyRef = efObject as PropertyRef ; if ( propertyRef != null && propertyRef . Parent is Key ) { var property = propertyRef . Name . Target ; if ( property != null ) { var entityType = property . Parent as Model . Entity . EntityType ; Debug . Assert ( entityType != null ) ; var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; var viewProperty = xref . GetExisting ( property ) as Property ; if ( viewEntityType != null && viewProperty != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , property ) ; } } return ; } var modelProperty = efObject . Parent as ComplexConceptualProperty ; if ( modelProperty != null ) { var entityType = modelProperty . Parent as Model . Entity . EntityType ; if ( entityType != null ) { var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; if ( viewEntityType != null ) { Debug . Assert ( xref . ContainsKey ( modelProperty ) , " " ) ; ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , modelProperty ) ; } } return ; } ModelElement modelElement = null ; var et = efObject as ConceptualEntityType ; if ( et != null ) { foreach ( var prop in et . Properties ( ) ) { modelElement = xref . GetExisting ( prop ) ; if ( modelElement != null ) { ModelXRef . Remove ( prop , modelElement ) ; modelElement . Delete ( ) ; } } foreach ( var prop in et . NavigationProperties ( ) ) { modelElement = xref . GetExisting ( prop ) ; if ( modelElement != null ) { ModelXRef . Remove ( prop , modelElement ) ; modelElement . Delete ( ) ; } } } var modelDiagram = efObject as ModelDesigner . Diagram ; var currentDiagram = GetDiagram ( ) ; if ( null != currentDiagram && null != modelDiagram && currentDiagram . DiagramId == modelDiagram . Id . Value ) { var foundAnotherActiveDiagram = false ; foreach ( var diagram in Store . ElementDirectory . FindElements < EntityDesignerDiagram > ( ) ) { if ( currentDiagram != diagram && diagram . ActiveDiagramView != null && diagram . IsDeleted == false && diagram . IsDeleting == false ) { foundAnotherActiveDiagram = true ; break ; } } if ( ! foundAnotherActiveDiagram ) { var service = EditingContext . GetEFArtifactService ( ) ; var entityDesignArtifact = service . Artifact as EntityDesignArtifact ; Debug . Assert ( entityDesignArtifact != null , " " ) ; if ( entityDesignArtifact != null ) { var firstModelDiagram = entityDesignArtifact . DesignerInfo . Diagrams . FirstDiagram ; Debug . Assert ( firstModelDiagram != null && firstModelDiagram != modelDiagram , " " ) ; if ( firstModelDiagram != null && firstModelDiagram != modelDiagram ) { currentDiagram . DiagramId = firstModelDiagram . Id . Value ; _shouldClearAndReloadDiagram = true ; return ; } } } } modelElement = xref . GetExisting ( efObject ) ; if ( modelElement != null ) { ModelXRef . Remove ( efObject , modelElement ) ; modelElement . Delete ( ) ; } }
Deletes corresponding ModelItem (if exists in the view model)
Deletes corresponding ModelItem ( if exists in the view model )
// deleting key means that we will have to re-translate the properties of the // EntityType because the propertyRefs were deleted // Only translate properties that are in the view model // deleting key's propertyref means removing property from entity's key // if we are deleting whole EntityType this will be null // If a diagram is deleted and the diagram is the only diagram that is opened in VS, we need to open the first diagram. // The assumption is that there will be another diagram exists in the model because the user can't delete a diagram if the diagram is the only diagram in the model. // We need to do this to ensure Model-Browser window is not closed and there at least 1 active designer for the EDMX in VS. // Check if there is any active diagram in VS. // firstModelDiagram should never be equal to the diagram that was deleted. // Change the DiagramID and reload. // Unfortunately we could not call ClearAndReloadDiagram here because a DSL transaction has been opened before this method is called;  // Need to make wait until the transaction is committed.
efObject
EFObject
xref
ModelToDesignerModelXRefItem