src/Misc/EFTools/EntityDesignEntityDesigner/CustomCode/DomainClasses/EntityDesignerViewModel.cs
/// <summary>
///     In general: the method update items if they are already created in the designer.
///     For Property element: creates if the entity-type exists.
///     For Association element: creates if both types exists.
/// </summary>
In general: the method update items if they are already created in the designer.
For Property element: creates if the entity-type exists.
For Association element: creates if both types exists.
In general : the method update items if they are already created in the designer . For Property element : creates if the entity - type exists . For Association element : creates if both types exists .
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        private void OnEFObjectCreatedOrUpdated(EFObject efObject, ModelToDesignerModelXRefItem xref)
        {
            Debug.Assert(efObject != null);
            if (false == ModelHelper.IsInConceptualModel(efObject))
            {
                return; 
            }
            var baseType = efObject as EntityTypeBaseType;
            if (baseType != null)
            {
                if (xref.ContainsKey(baseType.Parent))
                {
                    ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext).SynchronizeSingleDslModelElement(this, efObject);
                }
                return;
            }
            var efElement = efObject as EFElement;
            if (efElement == null)
            {
                efElement = efObject.Parent as EFElement;
            }
            if (efElement != null)
            {
                var modelRoot = efElement as ConceptualEntityModel;
                if (modelRoot != null)
                {
                    Namespace = modelRoot.Namespace.Value;
                    return;
                }
                var modelEntityType = efElement as ConceptualEntityType;
                if (modelEntityType != null)
                {
                    var viewEntityType = xref.GetExisting(modelEntityType) as EntityType;
                    if (viewEntityType != null)
                    {
                        ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                            .SynchronizeSingleDslModelElement(this, modelEntityType);
                    }
                    else if (efObject is ConceptualEntityType)
                    {
                        viewEntityType =
                            ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                .SynchronizeSingleDslModelElement(this, modelEntityType) as EntityType;
                        EntityTypes.Add(viewEntityType);
                    }
                    return;
                }
                var modelProperty = efElement as Model.Entity.Property;
                if (modelProperty != null)
                {
                    var entityType = modelProperty.Parent as Model.Entity.EntityType;
                    if (entityType != null)
                    {
                        var viewEntityType = xref.GetExisting(entityType) as EntityType;
                        if (viewEntityType != null)
                        {
                            var viewProperty = xref.GetExisting(modelProperty) as Property;
                            if (viewProperty != null)
                            {
                                ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                    .SynchronizeSingleDslModelElement(viewEntityType, modelProperty);
                            }
                            else
                            {
                                viewProperty =
                                    ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                        .SynchronizeSingleDslModelElement(viewEntityType, modelProperty) as Property;
                                Debug.Assert(viewProperty != null, "Why DSL property is null?");
                                if (viewProperty != null)
                                {
                                    var previousSibling = FindPreviousProperty(viewProperty) as Property;
                                    if (previousSibling != null)
                                    {
                                        var propertyIndex = viewEntityType.Properties.IndexOf(previousSibling);
                                        Debug.Assert(
                                            propertyIndex >= 0,
                                            "Unable to find index of DSL property: " + previousSibling.Name + " in entity:"
                                            + viewEntityType.Name);
                                        if (propertyIndex >= 0)
                                        {
                                            viewEntityType.Properties.Insert(propertyIndex + 1, viewProperty);
                                        }
                                    }
                                    else
                                    {
                                        viewEntityType.Properties.Insert(0, viewProperty);
                                    }
                                }
                            }
                        }
                    }
                    return;
                }
                var modelAssociationEnd = efElement as AssociationEnd;
                if (modelAssociationEnd != null)
                {
                    efElement = modelAssociationEnd.Parent as EFElement;
                }
                var modelAssociation = efElement as Model.Entity.Association;
                if (modelAssociation != null)
                {
                    ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                        .SynchronizeSingleDslModelElement(this, modelAssociation);
                    return;
                }
                var modelNavigationProperty = efElement as Model.Entity.NavigationProperty;
                if (modelNavigationProperty != null)
                {
                    var entityType = modelNavigationProperty.Parent as Model.Entity.EntityType;
                    Debug.Assert(entityType != null);
                    var viewEntityType = xref.GetExisting(entityType) as EntityType;
                    if (viewEntityType != null)
                    {
                        var viewNavigationProperty = xref.GetExisting(modelNavigationProperty) as NavigationProperty;
                        if (viewNavigationProperty != null)
                        {
                            ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                .SynchronizeSingleDslModelElement(viewEntityType, modelNavigationProperty);
                        }
                        else
                        {
                            viewNavigationProperty =
                                ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                    .SynchronizeSingleDslModelElement(viewEntityType, modelNavigationProperty) as NavigationProperty;
                            Debug.Assert(viewNavigationProperty != null, "Why DSL navigation property is null?");
                            if (viewNavigationProperty != null)
                            {
                                var previousSibling = FindPreviousProperty(viewNavigationProperty) as NavigationProperty;
                                if (previousSibling != null)
                                {
                                    var propertyIndex = viewEntityType.NavigationProperties.IndexOf(previousSibling);
                                    Debug.Assert(
                                        propertyIndex >= 0,
                                        "Unable to find index of DSL navigation property: " + previousSibling.Name + " in entity:"
                                        + viewEntityType.Name);
                                    if (propertyIndex >= 0)
                                    {
                                        viewEntityType.NavigationProperties.Insert(propertyIndex + 1, viewNavigationProperty);
                                    }
                                }
                                else
                                {
                                    viewEntityType.NavigationProperties.Insert(0, viewNavigationProperty);
                                }
                            }
                        }
                    }
                    return;
                }
                var key = efElement as Key;
                var propertyRefs = new List<PropertyRef>();
                if (key != null)
                {
                    foreach (var propRef in key.PropertyRefs)
                    {
                        propertyRefs.Add(propRef);
                    }
                }
                else
                {
                    var propertyRef = efElement as PropertyRef;
                    if (propertyRef != null)
                    {
                        propertyRefs.Add(propertyRef);
                    }
                }
                if (propertyRefs.Count > 0)
                {
                    foreach (var propRef in propertyRefs)
                    {
                        if (propRef != null
                            && propRef.Parent is Key)
                        {
                            var property = propRef.Name.Target;
                            if (property != null)
                            {
                                var entityType = property.Parent as Model.Entity.EntityType;
                                Debug.Assert(entityType != null);
                                var viewEntityType = xref.GetExisting(entityType) as EntityType;
                                if (viewEntityType != null)
                                {
                                    ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext)
                                        .SynchronizeSingleDslModelElement(viewEntityType, property);
                                }
                            }
                        }
                    }
                    return;
                }
            }
        }
[ SuppressMessage ( " " , " " ) ] [ SuppressMessage ( " " , " " ) ] private void OnEFObjectCreatedOrUpdated ( EFObject efObject , ModelToDesignerModelXRefItem xref ) { Debug . Assert ( efObject != null ) ; if ( false == ModelHelper . IsInConceptualModel ( efObject ) ) { return ; } var baseType = efObject as EntityTypeBaseType ; if ( baseType != null ) { if ( xref . ContainsKey ( baseType . Parent ) ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( this , efObject ) ; } return ; } var efElement = efObject as EFElement ; if ( efElement == null ) { efElement = efObject . Parent as EFElement ; } if ( efElement != null ) { var modelRoot = efElement as ConceptualEntityModel ; if ( modelRoot != null ) { Namespace = modelRoot . Namespace . Value ; return ; } var modelEntityType = efElement as ConceptualEntityType ; if ( modelEntityType != null ) { var viewEntityType = xref . GetExisting ( modelEntityType ) as EntityType ; if ( viewEntityType != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( this , modelEntityType ) ; } else if ( efObject is ConceptualEntityType ) { viewEntityType = ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( this , modelEntityType ) as EntityType ; EntityTypes . Add ( viewEntityType ) ; } return ; } var modelProperty = efElement as Model . Entity . Property ; if ( modelProperty != null ) { var entityType = modelProperty . Parent as Model . Entity . EntityType ; if ( entityType != null ) { var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; if ( viewEntityType != null ) { var viewProperty = xref . GetExisting ( modelProperty ) as Property ; if ( viewProperty != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , modelProperty ) ; } else { viewProperty = ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , modelProperty ) as Property ; Debug . Assert ( viewProperty != null , " " ) ; if ( viewProperty != null ) { var previousSibling = FindPreviousProperty ( viewProperty ) as Property ; if ( previousSibling != null ) { var propertyIndex = viewEntityType . Properties . IndexOf ( previousSibling ) ; Debug . Assert ( propertyIndex >= 0 , " " + previousSibling . Name + " " + viewEntityType . Name ) ; if ( propertyIndex >= 0 ) { viewEntityType . Properties . Insert ( propertyIndex + 1 , viewProperty ) ; } } else { viewEntityType . Properties . Insert ( 0 , viewProperty ) ; } } } } } return ; } var modelAssociationEnd = efElement as AssociationEnd ; if ( modelAssociationEnd != null ) { efElement = modelAssociationEnd . Parent as EFElement ; } var modelAssociation = efElement as Model . Entity . Association ; if ( modelAssociation != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( this , modelAssociation ) ; return ; } var modelNavigationProperty = efElement as Model . Entity . NavigationProperty ; if ( modelNavigationProperty != null ) { var entityType = modelNavigationProperty . Parent as Model . Entity . EntityType ; Debug . Assert ( entityType != null ) ; var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; if ( viewEntityType != null ) { var viewNavigationProperty = xref . GetExisting ( modelNavigationProperty ) as NavigationProperty ; if ( viewNavigationProperty != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , modelNavigationProperty ) ; } else { viewNavigationProperty = ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , modelNavigationProperty ) as NavigationProperty ; Debug . Assert ( viewNavigationProperty != null , " " ) ; if ( viewNavigationProperty != null ) { var previousSibling = FindPreviousProperty ( viewNavigationProperty ) as NavigationProperty ; if ( previousSibling != null ) { var propertyIndex = viewEntityType . NavigationProperties . IndexOf ( previousSibling ) ; Debug . Assert ( propertyIndex >= 0 , " " + previousSibling . Name + " " + viewEntityType . Name ) ; if ( propertyIndex >= 0 ) { viewEntityType . NavigationProperties . Insert ( propertyIndex + 1 , viewNavigationProperty ) ; } } else { viewEntityType . NavigationProperties . Insert ( 0 , viewNavigationProperty ) ; } } } } return ; } var key = efElement as Key ; var propertyRefs = new List < PropertyRef > ( ) ; if ( key != null ) { foreach ( var propRef in key . PropertyRefs ) { propertyRefs . Add ( propRef ) ; } } else { var propertyRef = efElement as PropertyRef ; if ( propertyRef != null ) { propertyRefs . Add ( propertyRef ) ; } } if ( propertyRefs . Count > 0 ) { foreach ( var propRef in propertyRefs ) { if ( propRef != null && propRef . Parent is Key ) { var property = propRef . Name . Target ; if ( property != null ) { var entityType = property . Parent as Model . Entity . EntityType ; Debug . Assert ( entityType != null ) ; var viewEntityType = xref . GetExisting ( entityType ) as EntityType ; if ( viewEntityType != null ) { ModelTranslatorContextItem . GetEntityModelTranslator ( EditingContext ) . SynchronizeSingleDslModelElement ( viewEntityType , property ) ; } } } } return ; } } }
In general: the method update items if they are already created in the designer.
In general : the method update items if they are already created in the designer .
// ignore events from storage model // special case for creating base type //update only // Check to see if EFObject is type of ConceptualEntityType. // This is to prevent that the scenario updating entity-type name causes a new entity-type to be created in different diagram. //create EntityType and add it to the view model // If the view Entity Type does not exist skip, continue since nothing to update. //update only //create property and add it to view's EntityType // Determine where we should insert the new property by getting the previous sibling. // Get the index of the previous sibling. // if previous sibling is null, that means we need to insert the property as the first property of the entity-type. // change focus to the association and it will be processed below // this will create association if necessary (if not it's update only) //update only //create navigation property and add it to view's EntityType // if previous sibling is null, that means we need to insert the property as the first navigation property of the entity-type. // see if we are creating the Key element or just a PropertyRef // if this is true, we are not necessarily creating the Key element with only one property ref element -  // we may be handling an undo operation, and there may be more than one property ref element.
efObject
EFObject
xref
ModelToDesignerModelXRefItem