/// <summary> /// Translate base type of a model EntityType into view Inheritance (creates an Inheritance if not yet created) /// </summary> /// <param name="viewModel"></param> /// <param name="entityType"></param> /// <returns></returns> private static Inheritance TranslateBaseType(EntityDesignerViewModel viewModel, ConceptualEntityType entityType) { if (entityType.BaseType.Status == BindingStatus.Known) { var baseType = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, entityType.BaseType.Target, viewModel.Partition) as ViewModelEntityType; var derivedType = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, entityType, viewModel.Partition) as ViewModelEntityType; // in Multiple diagram scenario, baseType and derivedType might not exist in the diagram. if (baseType != null && derivedType != null) { return (ModelToDesignerModelXRef.GetNewOrExisting(viewModel.EditingContext, entityType.BaseType, baseType, derivedType) as Inheritance); } } return(null); }
/// <summary> /// Translate model NavigationProperty into view NavigationProperty (creates a view NavigationProperty if not yet created) /// </summary> private ViewModelNavigationProperty TranslateNavigationProperty(ViewModelEntityType viewEntityType, ModelNavigationProperty navProp) { var viewNavProp = ModelToDesignerModelXRef.GetNewOrExisting(EditingContext, navProp, viewEntityType.Partition) as ViewModelNavigationProperty; Debug.Assert(viewNavProp != null, "Expected non-null navigation property"); viewNavProp.Name = navProp.LocalName.Value; if (navProp.Relationship.Status == BindingStatus.Known) { var association = ModelToDesignerModelXRef.GetExisting(EditingContext, navProp.Relationship.Target, viewEntityType.Partition) as ViewModelAssociation; // Association might be null here if the related entity does not exist in the current diagram. if (association != null) { viewNavProp.Association = association; // On a self-relationship case, we ensure that the Source and Target NavProps are not set with the same value. // The source is set first only if SourceEntity == TargetEntityType and the Source navprop has not been set yet. The other case would be // if this is not a self-relationship. if (viewEntityType == association.SourceEntityType && ((association.SourceEntityType == association.TargetEntityType && association.SourceNavigationProperty == null) || (association.SourceEntityType != association.TargetEntityType))) { association.SourceNavigationProperty = viewNavProp; } // SourceEntityType might be the same as TargetEntityType, so we need to check this as well if (viewEntityType == association.TargetEntityType) { association.TargetNavigationProperty = viewNavProp; } } } return(viewNavProp); }
/// <summary> /// Translate model Association into view Association (creates a view Association if not yet created) /// </summary> /// <param name="viewModel"></param> /// <param name="association"></param> /// <returns></returns> private static ViewModelAssociation TranslateAssociation( EntityDesignerViewModel viewModel, ModelAssociation association) { var ends = association.AssociationEnds(); var end1 = ends[0]; var end2 = ends[1]; if (end1.Type.Status == BindingStatus.Known && end2.Type.Status == BindingStatus.Known) { var viewEnd1 = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, end1.Type.Target, viewModel.Partition) as ViewModelEntityType; var viewEnd2 = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, end2.Type.Target, viewModel.Partition) as ViewModelEntityType; // Only create association if both entityType exist. if (viewEnd1 != null && viewEnd2 != null) { var viewAssoc = ModelToDesignerModelXRef.GetNewOrExisting(viewModel.EditingContext, association, viewEnd1, viewEnd2) as ViewModelAssociation; viewAssoc.Name = association.LocalName.Value; viewAssoc.SourceMultiplicity = end1.Multiplicity.Value; viewAssoc.TargetMultiplicity = end2.Multiplicity.Value; // There could be a situation where association is created after navigation property (for example: the user add an entity-type and then add related types ). // In that case we need to make sure that view's association and navigation property are linked. Debug.Assert( end1.Type.Target != null, "Association End: " + end1.DisplayName + " does not reference a valid entity-type."); if (end1.Type.Target != null) { var modelSourceNavigationProperty = ModelHelper.FindNavigationPropertyForAssociationEnd(end1.Type.Target as ConceptualEntityType, end1); if (modelSourceNavigationProperty != null) { var viewSourceNavigationProperty = ModelToDesignerModelXRef.GetExisting( viewModel.EditingContext, modelSourceNavigationProperty, viewModel.Partition) as ViewModelNavigationProperty; if (viewSourceNavigationProperty != null) { viewAssoc.SourceNavigationProperty = viewSourceNavigationProperty; viewSourceNavigationProperty.Association = viewAssoc; } } } Debug.Assert( end2.Type.Target != null, "Association End: " + end2.DisplayName + " does not reference a valid entity-type."); if (end2.Type.Target != null) { var modelTargetNavigatioNProperty = ModelHelper.FindNavigationPropertyForAssociationEnd(end2.Type.Target as ConceptualEntityType, end2); if (modelTargetNavigatioNProperty != null) { var viewTargetNavigationProperty = ModelToDesignerModelXRef.GetExisting( viewModel.EditingContext, modelTargetNavigatioNProperty, viewModel.Partition) as ViewModelNavigationProperty; if (viewTargetNavigationProperty != null) { viewAssoc.TargetNavigationProperty = viewTargetNavigationProperty; viewTargetNavigationProperty.Association = viewAssoc; } } } return(viewAssoc); } } return(null); }