예제 #1
0
        protected override void PostInvoke(CommandProcessorContext cpc)
        {
            // now that we have a new entity set, make sure that it and any derived types use correct MSL
            EnforceEntitySetMappingRules.AddRule(cpc, _derivedType.EntitySet);

            // see if this type is used by association ends; since we deleted the inheritance, this entity got
            // a new EntitySet so we need to change any EntitySet references for corresponding AssociationSetEnds to the new one
            Association association = null;

            foreach (var end in _derivedType.GetAntiDependenciesOfType <AssociationEnd>())
            {
                foreach (var setEnd in end.GetAntiDependenciesOfType <AssociationSetEnd>())
                {
                    setEnd.EntitySet.SetRefName(_derivedType.EntitySet);
                    XmlModelHelper.NormalizeAndResolve(setEnd);
                }

                association = end.Parent as Association;
            }

            // try to recreate the AssociationSetMapping if one exists
            if (association != null &&
                association.AssociationSet != null &&
                association.AssociationSet.AssociationSetMapping != null &&
                association.AssociationSet.AssociationSetMapping.XObject != null)
            {
                // store off the entity set for later
                var storeEntitySet = association.AssociationSet.AssociationSetMapping.StoreEntitySet.Target;

                // delete it
                DeleteEFElementCommand.DeleteInTransaction(cpc, association.AssociationSet.AssociationSetMapping);

                // create a new one (if we can)
                if (storeEntitySet != null &&
                    storeEntitySet.EntityType.Target != null)
                {
                    var set = storeEntitySet.EntityType.Target as StorageEntityType;
                    Debug.Assert(storeEntitySet.EntityType.Target == null || set != null, "EntityType is not StorageEntityType");

                    CreateAssociationSetMappingCommand.CreateAssociationSetMappingAndIntellimatch(cpc, association, set);
                }
            }

            base.PostInvoke(cpc);
        }
예제 #2
0
        /// <summary>
        ///     A helper method that creates the AssociationSetMapping and also tries to "Intelli-Match" some mappings, that is, if there are
        ///     columns and properties that match by name, we create mappings for the user.
        /// </summary>
        /// <param name="cpc"></param>
        /// <param name="association"></param>
        /// <param name="storageEntityType"></param>
        /// <returns></returns>
        internal static AssociationSetMapping CreateAssociationSetMappingAndIntellimatch(
            CommandProcessorContext cpc, Association association, StorageEntityType storageEntityType)
        {
            var associationSet = association.AssociationSet;

            Debug.Assert(associationSet != null, "An association found that doesn't have an association set");

            var cmd = new CreateAssociationSetMappingCommand(association, storageEntityType);

            CommandProcessor.InvokeSingleCommand(cpc, cmd);

            foreach (var setEnd in associationSet.AssociationSetEnds())
            {
                var builder = new AssociationSetEndMappingBuilderForCommand(setEnd, storageEntityType);
                builder.Build(cpc);
            }

            return(cmd.AssociationSetMapping);
        }
예제 #3
0
 internal CreateEndPropertyCommand(CreateAssociationSetMappingCommand prereq, AssociationSetEnd associationSetEnd)
     : base(PrereqId)
 {
     AssociationSetEnd = associationSetEnd;
     AddPreReqCommand(prereq);
 }
        protected override void PreInvoke(CommandProcessorContext cpc)
        {
            base.PreInvoke(cpc);

            // you cannot use this command with a type that already has a base class; throwing here
            // because its not a good idea to throw an exception in a c'tor
            if (EntityToBeDerived == null ||
                EntityToBeDerived.BaseType.Target != null)
            {
                throw new InvalidOperationException();
            }

            EnforceEntitySetMappingRules.AddRule(cpc, BaseType.EntitySet);

            // see if this type is used by association ends; since we are creating an inheritance
            // we need to change any AssociationSetEnd references from the current EntitySet to the
            // new baseType's EntitySet (the derived type's EntitySet will be deleted)
            var associationsToUpdate = new HashSet <Association>();

            if (EntityToBeDerived.EntitySet != null)
            {
                var associationSetEndsToUpdate = EntityToBeDerived.EntitySet.GetAntiDependenciesOfType <AssociationSetEnd>();
                foreach (var setEnd in associationSetEndsToUpdate)
                {
                    setEnd.EntitySet.SetRefName(BaseType.EntitySet);
                    XmlModelHelper.NormalizeAndResolve(setEnd);

                    var aSet = setEnd.Parent as AssociationSet;
                    if (aSet != null)
                    {
                        if (aSet.Association.Target != null)
                        {
                            associationsToUpdate.Add(aSet.Association.Target);
                        }
                    }
                }
            }

            foreach (var association in associationsToUpdate)
            {
                // try to recreate the AssociationSetMapping if one exists
                if (association != null &&
                    association.AssociationSet != null &&
                    association.AssociationSet.AssociationSetMapping != null &&
                    association.AssociationSet.AssociationSetMapping.XObject != null)
                {
                    // store off the entity set for later
                    var storeEntitySet = association.AssociationSet.AssociationSetMapping.StoreEntitySet.Target;

                    // delete it
                    DeleteEFElementCommand.DeleteInTransaction(cpc, association.AssociationSet.AssociationSetMapping);

                    // create a new one (if we can)
                    if (storeEntitySet != null &&
                        storeEntitySet.EntityType.Target != null)
                    {
                        var set = storeEntitySet.EntityType.Target as StorageEntityType;
                        Debug.Assert(set != null, "EntityType is not StorageEntityType");
                        CreateAssociationSetMappingCommand.CreateAssociationSetMappingAndIntellimatch(cpc, association, set);
                    }
                }
            }
        }
        /// <summary>
        ///     A helper method that creates the AssociationSetMapping and also tries to "Intelli-Match" some mappings, that is, if there are
        ///     columns and properties that match by name, we create mappings for the user.
        /// </summary>
        /// <param name="cpc"></param>
        /// <param name="association"></param>
        /// <param name="storageEntityType"></param>
        /// <returns></returns>
        internal static AssociationSetMapping CreateAssociationSetMappingAndIntellimatch(
            CommandProcessorContext cpc, Association association, StorageEntityType storageEntityType)
        {
            var associationSet = association.AssociationSet;
            Debug.Assert(associationSet != null, "An association found that doesn't have an association set");

            var cmd = new CreateAssociationSetMappingCommand(association, storageEntityType);
            CommandProcessor.InvokeSingleCommand(cpc, cmd);

            foreach (var setEnd in associationSet.AssociationSetEnds())
            {
                var builder = new AssociationSetEndMappingBuilderForCommand(setEnd, storageEntityType);
                builder.Build(cpc);
            }

            return cmd.AssociationSetMapping;
        }
        // <summary>
        //     NOTE: The association set mapping view model doesn't keep a reference to the mapping model item. Instead, it
        //     keeps it to the AssociationSet and then it can find the AssociationSetMapping as an anti-dep.  We don't need to clear
        //     or set the ModelItem property.
        // </summary>
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "The context argument cannot be null");
            Debug.Assert(AssociationSet.AssociationSetMapping == null, "Don't call this method if we already have a mapping");
            Debug.Assert(underlyingModelItem != null, "The underlyingModelItem cannot be null");
            var storeEntityType = underlyingModelItem as EntityType;
            Debug.Assert(
                storeEntityType != null,
                "underlyingModelItem must be of type EntityType, actual type = " + underlyingModelItem.GetType().FullName);
            Debug.Assert(!storeEntityType.EntityModel.IsCSDL, "The storageEntityType must not be a CSDL EntityType");

            Context = context;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateAssociationSetMapping);
            }

            // create the item
            var cmd1 = new CreateAssociationSetMappingCommand(MappingAssociation.Association, storeEntityType);

            // now try and do some match ups by name
            var cmd2 = new DelegateCommand(
                () =>
                    {
                        Parent.AddChild(this);

                        foreach (var child in Children)
                        {
                            var end = child as MappingAssociationSetEnd;
                            if (end != null)
                            {
                                foreach (var child2 in end.Children)
                                {
                                    var mesp = child2 as MappingEndScalarProperty;
                                    if (mesp != null)
                                    {
                                        var tableColumn =
                                            MappingAssociationSet.StorageEntityType.GetFirstNamedChildByLocalName(mesp.Property, true) as
                                            Property;
                                        if (tableColumn != null)
                                        {
                                            mesp.CreateModelItem(cpc, _context, tableColumn);
                                        }
                                    }
                                }
                            }
                        }
                    });

            // now make the change
            var cp = new CommandProcessor(cpc);
            cp.EnqueueCommand(cmd1);
            cp.EnqueueCommand(cmd2);
            cp.Invoke();
        }
 internal CreateEndPropertyCommand(CreateAssociationSetMappingCommand prereq, AssociationSetEnd associationSetEnd)
     : base(PrereqId)
 {
     AssociationSetEnd = associationSetEnd;
     AddPreReqCommand(prereq);
 }
        /// <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;
        }