internal CreateAssociationSetMappingCommand(
            EntityContainerMapping entityContainerMapping, AssociationSet associationSet, Association association,
            StorageEntitySet storageEntitySet)
            : base(PrereqId)
        {
            CommandValidation.ValidateEntityContainerMapping(entityContainerMapping);
            CommandValidation.ValidateAssociationSet(associationSet);
            CommandValidation.ValidateAssociation(association);
            CommandValidation.ValidateStorageEntitySet(storageEntitySet);

            EntityContainerMapping = entityContainerMapping;
            AssociationSet = associationSet;
            Association = association;
            StorageEntitySet = storageEntitySet;
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            Debug.Assert(Association != null, "InvokeInternal is called when Association is null.");
            if (Association == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when Association is null");
            }

            var service = cpc.EditingContext.GetEFArtifactService();
            var artifact = service.Artifact;

            // the entity container we want to add it to
            BaseEntityContainer entityContainer = null;
            switch (CommandModelSpace)
            {
                case ModelSpace.Conceptual:
                    entityContainer = artifact.ConceptualModel().FirstEntityContainer;
                    break;
                case ModelSpace.Storage:
                    entityContainer = artifact.StorageModel().FirstEntityContainer;
                    break;
            }
            Debug.Assert(entityContainer != null, "No entity container");

            // check for uniqueness
            string msg = null;
            if (ModelHelper.IsUniqueName(typeof(AssociationSet), entityContainer, Name, false, out msg) == false)
            {
                throw new InvalidOperationException(msg);
            }

            // create the new item in our model
            var associationSet = new AssociationSet(entityContainer, null);
            associationSet.LocalName.Value = Name;
            entityContainer.AddAssociationSet(associationSet);

            // set the association binding: needs to happen before the ends are resolved
            if (Association != null)
            {
                associationSet.Association.SetRefName(Association);
            }

            // TODO: what should we create if these bindings are unknown?
            var end1 = Association.AssociationEnds()[0];
            var end2 = Association.AssociationEnds()[1];
            if (end1 != null
                && end1.Type.Status == BindingStatus.Known
                && end2 != null
                && end2.Type.Status == BindingStatus.Known)
            {
                var setEnd1 = new AssociationSetEnd(associationSet, null);
                setEnd1.Role.SetRefName(end1);
                setEnd1.EntitySet.SetRefName(end1.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd1);

                var setEnd2 = new AssociationSetEnd(associationSet, null);
                setEnd2.Role.SetRefName(end2);
                setEnd2.EntitySet.SetRefName(end2.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd2);
            }

            XmlModelHelper.NormalizeAndResolve(associationSet);

            _createdAssociationSet = associationSet;
        }
 internal void AddAssociationSet(AssociationSet set)
 {
     _associationSets.Add(set);
 }
 internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
 {
     if (elem.Name.LocalName == AssociationSet.ElementName)
     {
         var assoc = new AssociationSet(this, elem);
         _associationSets.Add(assoc);
         assoc.Parse(unprocessedElements);
     }
     else
     {
         return base.ParseSingleElement(unprocessedElements, elem);
     }
     return true;
 }
 internal void AddAssociationSet(AssociationSet set)
 {
     _associationSets.Add(set);
 }
 public MappingAssociationSet(EditingContext context, AssociationSet assocSet, MappingEFElement parent)
     : base(context, assocSet, parent)
 {
 }
 internal static void ValidateAssociationSet(AssociationSet set)
 {
     ValidateEFElement(set);
 }
 public ExplorerAssociationSet(EditingContext context, AssociationSet assocSet, ExplorerEFElement parent)
     : base(context, assocSet, parent)
 {
     // do nothing
 }
 private ExplorerAssociationSet AddAssociationSet(AssociationSet entityType)
 {
     var explorerAssocSet =
         ModelToExplorerModelXRef.GetNew(_context, entityType, this, typeof(ExplorerAssociationSet)) as ExplorerAssociationSet;
     _associationSets.Insert(explorerAssocSet);
     return explorerAssocSet;
 }
        /// <summary>
        ///     Creates a new AssociationSetMapping in the existing EntityContainerMapping
        ///     based on another AssociationSetMapping (asmToClone) in a different artifact.
        ///     All the other parameters are presumed to already exist in the same artifact
        ///     as the EntityContainerMapping.
        /// </summary>
        private AssociationSetMapping CloneAssociationSetMapping(
            CommandProcessorContext cpc, AssociationSetMapping asmToClone,
            EntityContainerMapping existingEntityContainerMapping, AssociationSet existingAssociationSet,
            Association existingAssociation, StorageEntitySet existingStorageEntitySet,
            Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact)
        {
            var createASM = new CreateAssociationSetMappingCommand(
                existingEntityContainerMapping, existingAssociationSet, existingAssociation, existingStorageEntitySet);
            CommandProcessor.InvokeSingleCommand(cpc, createASM);
            var asmInExistingArtifact = createASM.AssociationSetMapping;

            if (null == asmInExistingArtifact)
            {
                throw new UpdateModelFromDatabaseException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UpdateFromDatabaseCannotCreateAssociationSetMapping,
                        existingAssociationSet.ToPrettyString()));
            }

            // cannot just look for an AssociationSetEnd with the same Role name in
            // the existing artifact as the role may have changed when the Association was
            // copied into the existing artifact - but we do know the ends were created in 
            // the same order - so simply match them up
            var existingAssocSetEnds = existingAssociationSet.AssociationSetEnds().ToArray();
            if (2 != existingAssocSetEnds.Length)
            {
                throw new UpdateModelFromDatabaseException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UpdateFromDatabaseAssociationSetMappingWrongNumberAssociationSetEnds,
                        existingAssociationSet.ToPrettyString(),
                        existingAssocSetEnds.Length));
            }

            var endsToClone = asmToClone.EndProperties().ToArray();
            if (2 != endsToClone.Length)
            {
                throw new UpdateModelFromDatabaseException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UpdateFromDatabaseAssociationSetMappingWrongNumberAssociationSetEnds,
                        existingAssociationSet.ToPrettyString(),
                        existingAssocSetEnds.Length));
            }

            for (var i = 0; i < 2; i++)
            {
                var aseInExistingArtifact = existingAssocSetEnds[i];
                var endToClone = endsToClone[i];
                CloneEndProperty(
                    cpc, endToClone, asmInExistingArtifact, aseInExistingArtifact, tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact);
            }

            return asmInExistingArtifact;
        }