Ejemplo n.º 1
0
        internal EntityTypeMapping Clone(EntitySetMapping newEntitySetMapping)
        {
            // here we clone the entity type mapping, instead of re-parenting it
            // this works around an XML editor bug where re-parenting an element causes asserts

            // first create the new XElement
            var tempDoc        = XDocument.Parse(XElement.ToString(SaveOptions.None), LoadOptions.None);
            var newetmXElement = tempDoc.Root;

            newetmXElement.Remove();
            // format the XML we just parsed
            Utils.FormatXML(newetmXElement, newEntitySetMapping.GetIndentLevel() + 1);

            // create the EntityTypeMapping & hook in it's xml.
            var newetm = new EntityTypeMapping(newEntitySetMapping, newetmXElement);

            newetm.AddXElementToParent(newetmXElement);

            // parse & Resolve the new EntityTypeMapping
            newetm.Parse(new HashSet <XName>());
            XmlModelHelper.NormalizeAndResolve(newetm);

            // add it to new EntitySetMapping
            newEntitySetMapping.AddEntityTypeMapping(newetm);

            return(newetm);
        }
Ejemplo n.º 2
0
 internal override bool ParseSingleElement(ICollection <XName> unprocessedElements, XElement elem)
 {
     if (elem.Name.LocalName == EntitySetMapping.ElementName)
     {
         var esm = new EntitySetMapping(this, elem);
         _entitySetMappings.Add(esm);
         esm.Parse(unprocessedElements);
     }
     else if (elem.Name.LocalName == AssociationSetMapping.ElementName)
     {
         var asm = new AssociationSetMapping(this, elem);
         _associationSetMappings.Add(asm);
         asm.Parse(unprocessedElements);
     }
     else if (elem.Name.LocalName == FunctionImportMapping.ElementName)
     {
         var fim = new FunctionImportMapping(this, elem);
         _functionImportMappings.Add(fim);
         fim.Parse(unprocessedElements);
     }
     else
     {
         return(base.ParseSingleElement(unprocessedElements, elem));
     }
     return(true);
 }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // if don't have an ECM yet, go create one
            if (_entityContainerMapping == null)
            {
                var cmd = new CreateEntityContainerMappingCommand(_entitySet.Artifact);
                CommandProcessor.InvokeSingleCommand(cpc, cmd);

                _entityContainerMapping = cmd.EntityContainerMapping;
            }

            Debug.Assert(_entityContainerMapping != null, "_entityContainerMapping should not be null");
            if (_entityContainerMapping == null)
            {
                throw new CannotLocateParentItemException();
            }

            // create the ESM
            var esm = new EntitySetMapping(_entityContainerMapping, null);
            esm.Name.SetRefName(_entitySet);
            _entityContainerMapping.AddEntitySetMapping(esm);

            XmlModelHelper.NormalizeAndResolve(esm);

            _created = esm;
        }
        /// <summary>
        ///     Creates an EntityTypeMapping in the passed in EntitySetMapping, for the passed in type and kind.
        /// </summary>
        /// <param name="entitySetMapping">If this is null, then an EntitySetMapping will be created.</param>
        /// <param name="entityType">This must be a valid EntityType from the C-Model.</param>
        /// <param name="kind">Which kind of ETM to create.</param>
        internal CreateEntityTypeMappingCommand(
            EntitySetMapping entitySetMapping, ConceptualEntityType entityType, EntityTypeMappingKind kind)
        {
            CommandValidation.ValidateEntitySetMapping(entitySetMapping);
            CommandValidation.ValidateConceptualEntityType(entityType);

            _entitySetMapping = entitySetMapping;
            _entityType = entityType;
            _kind = kind;
        }
Ejemplo n.º 5
0
 internal void AddEntitySetMapping(EntitySetMapping esm)
 {
     _entitySetMappings.Add(esm);
 }
 internal EnforceEntitySetMappingRules(CommandProcessorContext cpc, EntitySetMapping entitySetMapping)
 {
     _cpc = cpc;
     _entitySetMapping = entitySetMapping;
 }
 internal static void AddRule(CommandProcessorContext cpc, EntitySetMapping element)
 {
     if (element != null)
     {
         IIntegrityCheck check = new EnforceEntitySetMappingRules(cpc, element);
         cpc.AddIntegrityCheck(check);
     }
 }
Ejemplo n.º 8
0
        internal EntityTypeMapping Clone(EntitySetMapping newEntitySetMapping)
        {
            // here we clone the entity type mapping, instead of re-parenting it
            // this works around an XML editor bug where re-parenting an element causes asserts

            // first create the new XElement
            var tempDoc = XDocument.Parse(XElement.ToString(SaveOptions.None), LoadOptions.None);
            var newetmXElement = tempDoc.Root;
            newetmXElement.Remove();
            // format the XML we just parsed
            Utils.FormatXML(newetmXElement, newEntitySetMapping.GetIndentLevel() + 1);

            // create the EntityTypeMapping & hook in it's xml. 
            var newetm = new EntityTypeMapping(newEntitySetMapping, newetmXElement);
            newetm.AddXElementToParent(newetmXElement);

            // parse & Resolve the new EntityTypeMapping
            newetm.Parse(new HashSet<XName>());
            XmlModelHelper.NormalizeAndResolve(newetm);

            // add it to new EntitySetMapping
            newEntitySetMapping.AddEntityTypeMapping(newetm);

            return newetm;
        }
Ejemplo n.º 9
0
 internal static void ValidateEntitySetMapping(EntitySetMapping esm)
 {
     ValidateEFElement(esm);
     Debug.Assert(esm.Name.Target != null, "The passed in EntitySetMapping does not reference a known EntitySet");
 }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // make sure that there isn't an ETM of this kind already
            var entityTypeMapping = ModelHelper.FindEntityTypeMapping(cpc, _entityType, _kind, false);
            Debug.Assert(entityTypeMapping == null, "We are calling CreateEntityTypeMappingCommand and there is already one of this Kind");
            if (entityTypeMapping != null)
            {
                _created = entityTypeMapping;
                return;
            }

            // see if we can get the EntitySetMapping for our entity (if we weren't passed it)
            if (_entitySetMapping == null)
            {
                var ces = _entityType.EntitySet as ConceptualEntitySet;
                Debug.Assert(ces != null, "_entityType.EntitySet should be a ConceptualEntitySet");

                // find the EntitySetMapping for this type (V1 assumption is that there is only ESM per ES)
                EntitySetMapping esm = null;
                foreach (var depMapping in ces.GetAntiDependenciesOfType<EntitySetMapping>())
                {
                    esm = depMapping;
                    break;
                }

                _entitySetMapping = esm;
            }

            // if we still don't have an ESM, create one
            if (_entitySetMapping == null)
            {
                var cmd = new CreateEntitySetMappingCommand(
                    _entityType.Artifact.MappingModel().FirstEntityContainerMapping,
                    _entityType.EntitySet as ConceptualEntitySet);
                CommandProcessor.InvokeSingleCommand(cpc, cmd);
                _entitySetMapping = cmd.EntitySetMapping;
            }
            Debug.Assert(
                _entitySetMapping != null,
                "_entitySetMapping should not be null - we have been unable to find or create an EntitySetMapping");

            // create the ETM
            var etm = new EntityTypeMapping(_entitySetMapping, null, _kind);
            etm.TypeName.SetRefName(_entityType);
            _entitySetMapping.AddEntityTypeMapping(etm);

            XmlModelHelper.NormalizeAndResolve(etm);

            _created = etm;
        }
        /// <summary>
        ///     Creates a new EntityTypeMapping in the existing EntitySetMapping
        ///     based on another EntityTypeMapping (etmToClone) in a different artifact.
        ///     All the other parameters are presumed to already exist in the same artifact
        ///     as the EntitySetMapping.
        /// </summary>
        internal static EntityTypeMapping CloneEntityTypeMapping(
            CommandProcessorContext cpc,
            EntityTypeMapping etmToClone, EntitySetMapping existingEntitySetMapping,
            ConceptualEntityType existingEntityType, EntityTypeMappingKind kind)
        {
            var createETM = new CreateEntityTypeMappingCommand(existingEntitySetMapping, existingEntityType, kind);
            var cp = new CommandProcessor(cpc, createETM);
            cp.Invoke();

            var etm = createETM.EntityTypeMapping;

            foreach (var mappingFragment in etmToClone.MappingFragments())
            {
                var sesToClone = mappingFragment.StoreEntitySet.Target as StorageEntitySet;
                var ses = existingEntitySetMapping.EntityContainerMapping.Artifact.
                              StorageModel().FirstEntityContainer.GetFirstNamedChildByLocalName(sesToClone.LocalName.Value)
                          as StorageEntitySet;
                CreateMappingFragmentCommand.CloneMappingFragment(cpc, mappingFragment, etm, ses);
            }

            return etm;
        }
        /// <summary>
        ///     Creates a new EntitySetMapping in the existing EntityContainerMapping
        ///     based on another EntitySetMapping (esmToClone) in a different artifact.
        ///     Uses the FindMatchingEntityTypeInExistingArtifact() to match EntityTypes
        ///     within the EntitySetMapping.
        /// </summary>
        private EntitySetMapping CloneEntitySetMapping(
            CommandProcessorContext cpc, EntitySetMapping esmToClone,
            EntityContainerMapping existingEntityContainerMapping, ConceptualEntitySet existingEntitySet,
            Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact)
        {
            var createESM = new CreateEntitySetMappingCommand(existingEntityContainerMapping, existingEntitySet);
            CommandProcessor.InvokeSingleCommand(cpc, createESM);
            var esm = createESM.EntitySetMapping;

            foreach (var etmToBeCloned in esmToClone.EntityTypeMappings())
            {
                var bindings = etmToBeCloned.TypeName.Bindings;
                if (bindings.Count != 1)
                {
                    Debug.Fail(
                        "EntityTypeMapping to be cloned " + etmToBeCloned.ToPrettyString() +
                        " has bindings count = " + bindings.Count + ". We only support 1 binding.");
                }
                else
                {
                    var b = bindings.First();
                    var etToBeCloned = b.Target as ConceptualEntityType;
                    Debug.Assert(etToBeCloned != null, "EntityType target of binding is not ConceptualEntityType");
                    var etInExistingArtifact =
                        FindMatchingConceptualEntityTypeInExistingArtifact(
                            etToBeCloned,
                            tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact);
                    if (null != etInExistingArtifact)
                    {
                        CreateEntityTypeMappingCommand.CloneEntityTypeMapping(
                            cpc,
                            etmToBeCloned,
                            esm,
                            etInExistingArtifact,
                            etmToBeCloned.Kind);
                    }
                    else
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseEntitySetMappingCannotFindEntityType,
                                etToBeCloned.ToPrettyString()));
                    }
                }
            }

            return esm;
        }