src/Misc/EFTools/EntityDesignModel/Commands/UpdateConceptualAndMappingModelsCommand.cs
/// <summary>
///     Creates a new EndProperty in the existing AssociationSetMapping
///     based on another EndProperty (endToClone) in a different artifact.
///     All the other parameters are presumed to already exist in the same artifact
///     as the AssociationSetMapping.
/// </summary>
Creates a new EndProperty in the existing AssociationSetMapping
based on another EndProperty (endToClone) in a different artifact.
All the other parameters are presumed to already exist in the same artifact
as the AssociationSetMapping.
Creates a new EndProperty in the existing AssociationSetMapping based on another EndProperty ( endToClone ) in a different artifact . All the other parameters are presumed to already exist in the same artifact as the AssociationSetMapping .
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        private EndProperty CloneEndProperty(
            CommandProcessorContext cpc,
            EndProperty endToClone, AssociationSetMapping asmInExistingArtifact, AssociationSetEnd aseInExistingArtifact,
            Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact)
        {
            var createEnd = new CreateEndPropertyCommand(asmInExistingArtifact, aseInExistingArtifact);
            CommandProcessor.InvokeSingleCommand(cpc, createEnd);
            var endInExistingArtifact = createEnd.EndProperty;
            if (null == endInExistingArtifact)
            {
                throw new UpdateModelFromDatabaseException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UpdateFromDatabaseCannotCreateAssociationSetMappingEndProperty,
                        aseInExistingArtifact.ToPrettyString()));
            }
            var existingArtifact = cpc.Artifact;
            Debug.Assert(existingArtifact != null, "existingArtifact is null for endToClone " + endToClone.ToPrettyString());
            foreach (var sp in endToClone.ScalarProperties())
            {
                if (null == sp.Name.Target)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseScalarPropertyNoNameTarget,
                            sp.ToPrettyString()));
                }
                if (null == sp.ColumnName.Target)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseScalarPropertyNoColumnNameTarget,
                            sp.ToPrettyString()));
                }
                var spCSideEntityTypeinTempArtifact = sp.Name.Target.EntityType as ConceptualEntityType;
                if (null == spCSideEntityTypeinTempArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty,
                            sp.Name.Target.ToPrettyString()));
                }
                var spSSideEntityTypeinTempArtifact = sp.ColumnName.Target.EntityType as StorageEntityType;
                if (null == spSSideEntityTypeinTempArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty,
                            sp.ColumnName.Target.ToPrettyString()));
                }
                var csdlEntityTypeInExistingArtifact =
                    FindMatchingConceptualEntityTypeInExistingArtifact(
                        spCSideEntityTypeinTempArtifact,
                        tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact);
                if (null == csdlEntityTypeInExistingArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType,
                            sp.ToPrettyString(),
                            spCSideEntityTypeinTempArtifact.ToPrettyString()));
                }
                var ssdlEntityTypeInExistingArtifact =
                    FindMatchingStorageEntityTypeInExistingArtifact(spSSideEntityTypeinTempArtifact);
                if (null == ssdlEntityTypeInExistingArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType,
                            sp.ToPrettyString(),
                            spSSideEntityTypeinTempArtifact.ToPrettyString()));
                }
                var entityProperty = FindMatchingPropertyInExistingArtifactEntityType(sp.Name.Target, csdlEntityTypeInExistingArtifact);
                if (null == entityProperty)
                {
                    Property sSidePropertyToBeMappedInTempArtifact = null;
                    foreach (var spInTempArtifact in sp.Name.Target.GetAntiDependenciesOfType<ScalarProperty>())
                    {
                        if (null != spInTempArtifact.GetParentOfType(typeof(EntitySetMapping)))
                        {
                            if (null != spInTempArtifact.ColumnName
                                && null != spInTempArtifact.ColumnName.Target)
                            {
                                if (null == sSidePropertyToBeMappedInTempArtifact)
                                {
                                    sSidePropertyToBeMappedInTempArtifact = spInTempArtifact.ColumnName.Target;
                                }
                                else
                                {
                                    Debug.Fail(
                                        "C-side Property " + sp.Name.Target.ToPrettyString() +
                                        " has more than 1 ScalarProperty anti-dep with an EntitySetMapping parent. Should be at most 1.");
                                    break;
                                }
                            }
                        }
                    }
                    if (null == sSidePropertyToBeMappedInTempArtifact)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindSSideForCSideProperty,
                                sp.ToPrettyString(),
                                sp.Name.Target.ToPrettyString()));
                    }
                    var sSidePropertyToBeMappedInExistingArtifact =
                        FindSSidePropInExistingArtifact(sSidePropertyToBeMappedInTempArtifact);
                    if (null == sSidePropertyToBeMappedInExistingArtifact)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingSSideProperty,
                                sp.ToPrettyString(),
                                sSidePropertyToBeMappedInTempArtifact.ToPrettyString()));
                    }
                    entityProperty = CreateNewConceptualPropertyAndMapping(
                        cpc, sp.Name.Target, sSidePropertyToBeMappedInTempArtifact,
                        csdlEntityTypeInExistingArtifact);
                    if (null == entityProperty)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindOrCreateMatchingProperty,
                                sp.ToPrettyString(),
                                sp.Name.Target.ToPrettyString(),
                                csdlEntityTypeInExistingArtifact.ToPrettyString()));
                    }
                }
                var tableColumn = FindMatchingPropertyInExistingArtifactEntityType(sp.ColumnName.Target, ssdlEntityTypeInExistingArtifact);
                if (null == tableColumn)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingProperty,
                            sp.ToPrettyString(),
                            sp.ColumnName.Target.ToPrettyString(),
                            ssdlEntityTypeInExistingArtifact.ToPrettyString()));
                }
                var createScalar = new CreateEndScalarPropertyCommand(endInExistingArtifact, entityProperty, tableColumn);
                CommandProcessor.InvokeSingleCommand(cpc, createScalar);
                var existingScalarProp = createScalar.ScalarProperty;
                if (null == existingScalarProp)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseCannotCreateAssociationSetMappingScalarProperty,
                            entityProperty.ToPrettyString(),
                            tableColumn.ToPrettyString()));
                }
            }
            return endInExistingArtifact;
        }
[ SuppressMessage ( " " , " " ) ] private EndProperty CloneEndProperty ( CommandProcessorContext cpc , EndProperty endToClone , AssociationSetMapping asmInExistingArtifact , AssociationSetEnd aseInExistingArtifact , Dictionary < EntityType , EntityType > tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact ) { var createEnd = new CreateEndPropertyCommand ( asmInExistingArtifact , aseInExistingArtifact ) ; CommandProcessor . InvokeSingleCommand ( cpc , createEnd ) ; var endInExistingArtifact = createEnd . EndProperty ; if ( null == endInExistingArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseCannotCreateAssociationSetMappingEndProperty , aseInExistingArtifact . ToPrettyString ( ) ) ) ; } var existingArtifact = cpc . Artifact ; Debug . Assert ( existingArtifact != null , " " + endToClone . ToPrettyString ( ) ) ; foreach ( var sp in endToClone . ScalarProperties ( ) ) { if ( null == sp . Name . Target ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseScalarPropertyNoNameTarget , sp . ToPrettyString ( ) ) ) ; } if ( null == sp . ColumnName . Target ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseScalarPropertyNoColumnNameTarget , sp . ToPrettyString ( ) ) ) ; } var spCSideEntityTypeinTempArtifact = sp . Name . Target . EntityType as ConceptualEntityType ; if ( null == spCSideEntityTypeinTempArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty , sp . Name . Target . ToPrettyString ( ) ) ) ; } var spSSideEntityTypeinTempArtifact = sp . ColumnName . Target . EntityType as StorageEntityType ; if ( null == spSSideEntityTypeinTempArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty , sp . ColumnName . Target . ToPrettyString ( ) ) ) ; } var csdlEntityTypeInExistingArtifact = FindMatchingConceptualEntityTypeInExistingArtifact ( spCSideEntityTypeinTempArtifact , tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact ) ; if ( null == csdlEntityTypeInExistingArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType , sp . ToPrettyString ( ) , spCSideEntityTypeinTempArtifact . ToPrettyString ( ) ) ) ; } var ssdlEntityTypeInExistingArtifact = FindMatchingStorageEntityTypeInExistingArtifact ( spSSideEntityTypeinTempArtifact ) ; if ( null == ssdlEntityTypeInExistingArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType , sp . ToPrettyString ( ) , spSSideEntityTypeinTempArtifact . ToPrettyString ( ) ) ) ; } var entityProperty = FindMatchingPropertyInExistingArtifactEntityType ( sp . Name . Target , csdlEntityTypeInExistingArtifact ) ; if ( null == entityProperty ) { Property sSidePropertyToBeMappedInTempArtifact = null ; foreach ( var spInTempArtifact in sp . Name . Target . GetAntiDependenciesOfType < ScalarProperty > ( ) ) { if ( null != spInTempArtifact . GetParentOfType ( typeof ( EntitySetMapping ) ) ) { if ( null != spInTempArtifact . ColumnName && null != spInTempArtifact . ColumnName . Target ) { if ( null == sSidePropertyToBeMappedInTempArtifact ) { sSidePropertyToBeMappedInTempArtifact = spInTempArtifact . ColumnName . Target ; } else { Debug . Fail ( " " + sp . Name . Target . ToPrettyString ( ) + " " ) ; break ; } } } } if ( null == sSidePropertyToBeMappedInTempArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindSSideForCSideProperty , sp . ToPrettyString ( ) , sp . Name . Target . ToPrettyString ( ) ) ) ; } var sSidePropertyToBeMappedInExistingArtifact = FindSSidePropInExistingArtifact ( sSidePropertyToBeMappedInTempArtifact ) ; if ( null == sSidePropertyToBeMappedInExistingArtifact ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindMatchingSSideProperty , sp . ToPrettyString ( ) , sSidePropertyToBeMappedInTempArtifact . ToPrettyString ( ) ) ) ; } entityProperty = CreateNewConceptualPropertyAndMapping ( cpc , sp . Name . Target , sSidePropertyToBeMappedInTempArtifact , csdlEntityTypeInExistingArtifact ) ; if ( null == entityProperty ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindOrCreateMatchingProperty , sp . ToPrettyString ( ) , sp . Name . Target . ToPrettyString ( ) , csdlEntityTypeInExistingArtifact . ToPrettyString ( ) ) ) ; } } var tableColumn = FindMatchingPropertyInExistingArtifactEntityType ( sp . ColumnName . Target , ssdlEntityTypeInExistingArtifact ) ; if ( null == tableColumn ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseAssociationSetMappingCannotFindMatchingProperty , sp . ToPrettyString ( ) , sp . ColumnName . Target . ToPrettyString ( ) , ssdlEntityTypeInExistingArtifact . ToPrettyString ( ) ) ) ; } var createScalar = new CreateEndScalarPropertyCommand ( endInExistingArtifact , entityProperty , tableColumn ) ; CommandProcessor . InvokeSingleCommand ( cpc , createScalar ) ; var existingScalarProp = createScalar . ScalarProperty ; if ( null == existingScalarProp ) { throw new UpdateModelFromDatabaseException ( string . Format ( CultureInfo . CurrentCulture , Resources . UpdateFromDatabaseCannotCreateAssociationSetMappingScalarProperty , entityProperty . ToPrettyString ( ) , tableColumn . ToPrettyString ( ) ) ) ; } } return endInExistingArtifact ; }
Creates a new EndProperty in the existing AssociationSetMapping
based on another EndProperty (endToClone) in a different artifact.
Creates a new EndProperty in the existing AssociationSetMapping based on another EndProperty ( endToClone ) in a different artifact .
// Cannot find matching property - it must have been unmapped.  // So try to create a new mapped property to which to attach  // this association. // First find S-side Property in temp artifact to which the C-side // Property identified in the AssociationSetMapping is mapped // (Note: cannot use just sp.ColumnName.Target as the S-side Property // used in the AssociationSetMapping can be different from what is used // for the EntitySetMapping in the temp artifact and it is this latter // we need to replicate here). // Ensure that S-side ScalarProperty is from an EntitySetMapping (and not  // an AssociationSetMapping) in the temp artifact. // Can use first one as in temp artifact there is 1:1 mapping. // error in temp artifact - there's more than 1 EntitySetMapping ScalarProperty // mapped to the C-side Property // Now find the matching S-side Property in the existing artifact // Now create a new C-side Property in the existing artifact mapped  // to the S-side Property we just found
cpc
CommandProcessorContext
endToClone
EndProperty
asmInExistingArtifact
AssociationSetMapping
aseInExistingArtifact
AssociationSetEnd
tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact
Dictionary<EntityType, EntityType>