src/Misc/EFTools/EntityDesignEntityDesigner/CustomCode/CustomSerializer/EntityModelToDslModelTranslatorStrategy.cs
/// <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>
Translate model Association into view Association (creates a view Association if not yet created)
Translate model Association into view Association ( creates a view Association if not yet created )
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;
                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;
                    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;
        }
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 ; 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 ; Debug . Assert ( end1 . Type . Target != null , " " + end1 . DisplayName + " " ) ; 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 , " " + end2 . DisplayName + " " ) ; 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 ; }
Translate model Association into view Association (creates a view Association if not yet created)
Translate model Association into view Association ( creates a view Association if not yet created )
// Only create association if both entityType exist. // 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.
None
viewModel
EntityDesignerViewModel
None
association
ModelAssociation
None