Exemple #1
0
        /// <summary>
        /// Returns all store entitysets mapped to the specified conceptual model entity type
        /// </summary>
        /// <param name="modelEntityType">A conceptual model entity type</param>
        /// <returns>An enumeration of the store entitysets mapped to the specified conceptual model entity type</returns>
        public IEnumerable <StoreEntitySet> StoreEntitySetsFor(ModelEntityType modelEntityType)
        {
            XmlElement entityTypeMapping = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);

            if (entityTypeMapping != null)
            {
                foreach (XmlElement mappingFragment in entityTypeMapping.SelectNodes("map:MappingFragment", NSM))
                {
                    string         storeEntitySetName = mappingFragment.GetAttribute("StoreEntitySet");
                    StoreEntitySet storeEntitySet     = StoreEntitySets.FirstOrDefault(ses => ses.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));
                    if (storeEntitySet != null)
                    {
                        yield return(storeEntitySet);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Adds a new entity type mapping to this entity set mapping. Used for adding inherited sub-types to an entity set mapping
        /// </summary>
        /// <param name="modelEntityType">Conceptual model entity type to add to the mapping</param>
        /// <param name="storeEntityType">Store entity type mapped to the conceptual model entity type</param>
        public void AddEntityTypeMapping(ModelEntityType modelEntityType, StoreEntityType storeEntityType)
        {
            string storeEntitySetName = storeEntityType.EntitySet.Name;

            //get hold of the type mapping
            XmlElement entityTypeMapping = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);

            if (entityTypeMapping == null)
            {
                //not found - create
                entityTypeMapping = EDMXDocument.CreateElement("EntityTypeMapping", NameSpaceURImap);
                _esmElement.AppendChild(entityTypeMapping);

                if ((modelEntityType.HasBaseType || modelEntityType.HasSubTypes) &&
                    this.ModelEntitySet.InheritanceStrategy != EDMXInheritanceStrategyEnum.TPC)
                {
                    entityTypeMapping.SetAttribute("TypeName", "IsTypeOf(" + modelEntityType.FullName + ")");
                }
                else
                {
                    entityTypeMapping.SetAttribute("TypeName", modelEntityType.FullName);
                }
            }

            XmlElement mappingFragment = (XmlElement)entityTypeMapping.SelectSingleNode("map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);

            if (mappingFragment == null)
            {
                mappingFragment = EDMXDocument.CreateElement("MappingFragment", NameSpaceURImap);
                entityTypeMapping.AppendChild(mappingFragment);

                mappingFragment.SetAttribute("StoreEntitySet", storeEntitySetName);

                if (_storeEntitySetsEnumerated == true)
                {
                    StoreEntitySet storeEntitySet = _csMapping.ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name == storeEntitySetName);
                    if (storeEntitySet != null)
                    {
                        storeEntitySet.Removed += new EventHandler(storeEntitySet_Removed);
                        _storeEntitySets.Add(storeEntitySet);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns the entity type mapped to the specified store entity set
        /// </summary>
        /// <param name="storeEntitySet">A store entityset that is mapped with this EntitySetMapping</param>
        /// <returns>An entity type object for the entity type mapped to the specified store entityset</returns>
        public ModelEntityType EntityTypeFor(StoreEntitySet storeEntitySet)
        {
            string     storeEntitySetName = storeEntitySet.Name;
            XmlElement mappingFragment    = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping/map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);

            if (mappingFragment != null)
            {
                string entityTypeName = EDMXUtils.StripTypeOf(((XmlElement)mappingFragment.ParentNode).GetAttribute("TypeName"));

                ModelEntityType entityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName == entityTypeName || et.AliasName == entityTypeName);
                return(entityType);
            }
            else
            {
                return(null);
            }
        }
Exemple #4
0
        /// <summary>
        /// Adds a complex type mapping
        /// </summary>
        /// <param name="complexTypeReference">Model member property referencing the complex type property</param>
        /// <param name="memberProperty">Model member property</param>
        /// <param name="storeMemberProperty">Store member property</param>
        public void AddComplexMapping(ModelMemberProperty complexTypeReference, ModelMemberProperty memberProperty, StoreMemberProperty storeMemberProperty)
        {
            //find the appropriate mapping fragment
            string storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name;

            foreach (XmlElement mappingFragment in _esmElement.SelectNodes("map:EntityTypeMapping/map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM))
            {
                if (mappingFragment != null)
                {
                    XmlElement complexProperty = (XmlElement)mappingFragment.SelectSingleNode("map:ComplexProperty[@Name=" + XmlHelpers.XPathLiteral(complexTypeReference.Name) + "]", NSM);
                    if (complexProperty == null)
                    {
                        complexProperty = EDMXDocument.CreateElement("ComplexProperty", NameSpaceURImap);
                        complexProperty.SetAttribute("Name", complexTypeReference.Name);
                        complexProperty.SetAttribute("TypeName", complexTypeReference.TypeName);
                        mappingFragment.AppendChild(complexProperty);
                    }

                    string          entityTypeName = EDMXUtils.StripTypeOf(((XmlElement)mappingFragment.ParentNode).GetAttribute("TypeName"));
                    ModelEntityType entityType     = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName == entityTypeName || et.AliasName == entityTypeName);

                    XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                    scalarProperty.SetAttribute("Name", memberProperty.Name);
                    scalarProperty.SetAttribute("ColumnName", storeMemberProperty.Name);
                    complexProperty.AppendChild(scalarProperty);

                    _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(storeMemberProperty, memberProperty, entityType));

                    storeMemberProperty.Removed += new EventHandler(smp_Removed);
                    memberProperty.Removed      += new EventHandler(mmp_Removed);
                }
                else
                {
                    throw new ArgumentException("The store entity set " + storeEntitySetName + " is not associated with the model entity set " + this.ModelEntitySet.Name);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Removes all scalar member mappings for a specific conceptual model member.
        /// </summary>
        /// <param name="modelMemberProperty">Conceptual model member to remove mappings for.</param>
        public void RemoveMemberMapping(ModelMemberProperty modelMemberProperty)
        {
            _memberMappings.RemoveAll(mm => mm.Item2 == modelMemberProperty);

            foreach (XmlElement scalarProperty in _esmElement.SelectNodes("map:EntityTypeMapping/map:MappingFragment//map:ScalarProperty[@Name=" + XmlHelpers.XPathLiteral(modelMemberProperty.Name) + "]", NSM))
            {
                XmlNode parentNode = scalarProperty.ParentNode;
                if (parentNode != null)
                {
                    parentNode.RemoveChild(scalarProperty);

                    //if this was the last child node, remove the wrapper
                    if (!parentNode.HasChildNodes)
                    {
                        parentNode.ParentNode.RemoveChild(parentNode);
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Removes all scalar member mappings for a specific storage model member.
        /// </summary>
        /// <param name="storeMemberProperty">Storage model member to remove mappings for.</param>
        public void RemoveMemberMapping(StoreMemberProperty storeMemberProperty)
        {
            _memberMappings.RemoveAll(mm => mm.Item1 == storeMemberProperty);

            string     storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name;
            XmlElement mappingFragment    = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping/map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);

            if (mappingFragment != null)
            {
                XmlElement scalarProperty = (XmlElement)mappingFragment.SelectSingleNode(".//map:ScalarProperty[@ColumnName=" + XmlHelpers.XPathLiteral(storeMemberProperty.Name) + "]", NSM);
                if (scalarProperty != null)
                {
                    XmlNode parentNode = scalarProperty.ParentNode;
                    if (parentNode != null)
                    {
                        parentNode.RemoveChild(scalarProperty);

                        //if this was the last child node, remove the wrapper
                        if (!parentNode.HasChildNodes)
                        {
                            parentNode.ParentNode.RemoveChild(parentNode);
                        }
                    }
                }
            }
        }
Exemple #7
0
 internal void UpdateKeyName(ModelEntityType entityType, ModelMemberProperty memberProperty, string oldName, string newName)
 {
     try
     {
         if (entityType == FromEntitySet.EntityType)
         {
             foreach (XmlElement key in _associationElement.SelectNodes("edm:ReferentialConstraint/edm:Dependent/edm:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(oldName) + "]", NSM))
             {
                 key.SetAttribute("Name", newName);
             }
         }
         else if (entityType == ToEntitySet.EntityType)
         {
             foreach (XmlElement key in _associationElement.SelectNodes("edm:ReferentialConstraint/edm:Principal/edm:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(oldName) + "]", NSM))
             {
                 key.SetAttribute("Name", newName);
             }
         }
         else
         {
             throw new ArgumentException("The entity type " + entityType.Name + " does not participate in the association " + this.Name);
         }
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
Exemple #8
0
        /// <summary>
        /// Adds a member mapping from a conceptual model scalar member to a storage model scalar member, with a entity type specified
        /// </summary>
        /// <param name="modelMemberProperty">Conceptual model scalar member to map</param>
        /// <param name="storeMemberProperty">Storage model scalar member to map to</param>
        /// <param name="modelEntityType">Model entity type to specify in the EntityTypeMapping for this member mapping.</param>
        public void AddMemberMapping(ModelMemberProperty modelMemberProperty, StoreMemberProperty storeMemberProperty, ModelEntityType modelEntityType)
        {
            if (modelEntityType != _modelEntitySet.EntityType && !modelEntityType.IsSubtypeOf(_modelEntitySet.EntityType))
            {
                throw new ArgumentException("The model member does not belong to the mapped entity type or a subclass of the mapped entity type.");
            }

            if (storeMemberProperty.EntityType.EntitySet != null)
            {
                //find the appropriate mapping fragment
                string storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name;

                //get hold of the type mapping
                XmlElement entityTypeMapping = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);
                if (entityTypeMapping == null)
                {
                    //not found - create
                    entityTypeMapping = EDMXDocument.CreateElement("EntityTypeMapping", NameSpaceURImap);
                    _esmElement.AppendChild(entityTypeMapping);

                    entityTypeMapping.SetAttribute("TypeName", modelEntityType.FullName);
                }

                XmlElement mappingFragment = (XmlElement)entityTypeMapping.SelectSingleNode("map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);
                if (mappingFragment == null)
                {
                    mappingFragment = EDMXDocument.CreateElement("MappingFragment", NameSpaceURImap);
                    entityTypeMapping.AppendChild(mappingFragment);

                    mappingFragment.SetAttribute("StoreEntitySet", storeEntitySetName);

                    if (_storeEntitySetsEnumerated == true)
                    {
                        StoreEntitySet storeEntitySet = _csMapping.ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));
                        if (storeEntitySet != null)
                        {
                            storeEntitySet.Removed += new EventHandler(storeEntitySet_Removed);
                            _storeEntitySets.Add(storeEntitySet);
                        }
                    }
                }

                if (mappingFragment != null)
                {
                    if (mappingFragment.SelectSingleNode("map:ScalarProperty[@Name=" + XmlHelpers.XPathLiteral(modelMemberProperty.Name) + "][@ColumnName=" + XmlHelpers.XPathLiteral(storeMemberProperty.Name) + "]", NSM) == null)
                    {
                        XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                        scalarProperty.SetAttribute("Name", modelMemberProperty.Name);
                        scalarProperty.SetAttribute("ColumnName", storeMemberProperty.Name);
                        mappingFragment.AppendChild(scalarProperty);

                        _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(storeMemberProperty, modelMemberProperty, modelEntityType));

                        storeMemberProperty.Removed += new EventHandler(smp_Removed);
                        modelMemberProperty.Removed += new EventHandler(mmp_Removed);

                        storeMemberProperty.CSMappingsUpdated();
                        modelMemberProperty.CSMappingsUpdated();
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                }
                else
                {
                    throw new ArgumentException("The store entity set " + storeEntitySetName + " is not associated with the model entity set " + this.ModelEntitySet.Name);
                }
            }
            else
            {
                throw new InvalidOperationException("The store entity type " + (storeMemberProperty.EntityType != null ? storeMemberProperty.EntityType.Name : "[unknown]") + " is not associated with an entity set.");
            }
        }
Exemple #9
0
        internal ModelAssociationSet(EDMXFile parentFile, ConceptualModel conceptualModel, XmlElement associationSetElement)
            : base(parentFile)
        {
            _conceptualModel       = conceptualModel;
            _associationSetElement = associationSetElement;

            string associationName = _associationSetElement.GetAttribute("Association");

            if (!string.IsNullOrEmpty(associationName))
            {
                if (_associationSetElement.ParentNode != null && _associationSetElement.ParentNode.ParentNode != null)
                {
                    XmlElement schemaElement   = (XmlElement)_associationSetElement.ParentNode.ParentNode;
                    string     schemaNamespace = schemaElement.GetAttribute("Namespace");
                    string     schemaAlias     = schemaElement.GetAttribute("Alias");
                    if (!string.IsNullOrEmpty(schemaNamespace) && associationName.StartsWith(schemaNamespace))
                    {
                        associationName = associationName.Substring(schemaNamespace.Length + 1);
                    }
                    else if (!string.IsNullOrEmpty(schemaAlias) && associationName.StartsWith(schemaAlias))
                    {
                        associationName = associationName.Substring(schemaAlias.Length + 1);
                    }
                    _associationElement = (XmlElement)_associationSetElement.ParentNode.ParentNode.SelectSingleNode("edm:Association[@Name=" + XmlHelpers.XPathLiteral(associationName) + "]", NSM);
                }
            }

            if (_associationElement == null)
            {
                throw new InvalidModelObjectException("The ModelAssociationSet " + (associationName ?? "[unknown]") + " has no corresponding association element.");
            }
        }
Exemple #10
0
        /// <summary>
        /// Removes a key pair from the association
        /// </summary>
        /// <param name="key">A tuple containing the dependent and principal key member to remove</param>
        public void RemoveKey(Tuple <ModelMemberProperty, ModelMemberProperty> key)
        {
            try
            {
                if (_keys.Contains(key))
                {
                    _keys.Remove(key);
                }

                XmlElement fromKeyElement = (XmlElement)_associationElement.SelectSingleNode("edm:ReferentialConstraint/edm:Dependent/edm:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(key.Item1.Name) + "]", NSM);
                if (fromKeyElement != null)
                {
                    if (fromKeyElement.ParentNode != null)
                    {
                        if (fromKeyElement.ParentNode != null)
                        {
                            fromKeyElement.ParentNode.RemoveChild(fromKeyElement);
                        }
                    }
                }

                XmlElement toKeyElement = (XmlElement)_associationElement.SelectSingleNode("edm:ReferentialConstraint/edm:Principal/edm:PropertyRef", NSM);
                if (toKeyElement != null)
                {
                    if (toKeyElement.ParentNode != null)
                    {
                        if (toKeyElement.ParentNode != null)
                        {
                            toKeyElement.ParentNode.RemoveChild(toKeyElement);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    ExceptionTools.AddExceptionData(ex, this);
                }
                catch { }
                throw;
            }
        }
Exemple #11
0
        internal MappingCondition(EDMXFile parentFile, EntitySetMapping entitySetMapping, XmlElement entitySetMappingElement, ModelEntityType modelEntityType, StoreMemberProperty discriminatorColumn, string discriminatorValue)
            : base(parentFile)
        {
            _entitySetMapping    = entitySetMapping;
            _modelEntityType     = modelEntityType;
            _discriminatorColumn = discriminatorColumn;

            string storeEntitySetName = discriminatorColumn.EntityType.EntitySet.Name;

            //get hold of the type mapping
            _entityTypeMapping = (XmlElement)entitySetMappingElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);
            if (_entityTypeMapping == null)
            {
                throw new ArgumentException("The entity type " + modelEntityType.Name + " is not a participant in this entity set mapping.");
            }

            _mappingFragment = (XmlElement)_entityTypeMapping.SelectSingleNode("map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);
            if (_mappingFragment == null)
            {
                throw new ArgumentException("The store entityset " + storeEntitySetName + " is not a participant in this entity set mapping.");
            }

            _mappingCondition = EDMXDocument.CreateElement("Condition", NameSpaceURImap);
            _mappingCondition.SetAttribute("ColumnName", discriminatorColumn.Name);
            if (discriminatorValue != null)
            {
                _mappingCondition.SetAttribute("Value", discriminatorValue);
            }
            else
            {
                _mappingCondition.SetAttribute("IsNull", "true");
            }
            _mappingFragment.AppendChild(_mappingCondition);
        }
Exemple #12
0
        internal void SetOption(string name, bool value)
        {
            XmlElement propertyElement = (XmlElement)_optionsPropertySetElement.SelectSingleNode("edmx:DesignerProperty[@Name=" + XmlHelpers.XPathLiteral(name) + "]", NSM);

            if (propertyElement == null)
            {
                propertyElement = _document.CreateElement("DesignerProperty", NameSpaceURIedmx);
                propertyElement.SetAttribute("Name", name);
            }
            propertyElement.SetAttribute("Value", value.ToLString());
        }
Exemple #13
0
        internal bool GetOption(string name)
        {
            XmlElement propertyElement = (XmlElement)_optionsPropertySetElement.SelectSingleNode("edmx:DesignerProperty[@Name=" + XmlHelpers.XPathLiteral(name) + "]", NSM);

            return(propertyElement.GetAttribute("Value").Equals("true", StringComparison.InvariantCultureIgnoreCase));
        }
        internal void UpdateKeyName(StoreEntityType entityType, StoreMemberProperty memberProperty, string oldName, string newName)
        {
            if (_associationElement == null)
            {
                throw new InvalidOperationException("The association set doesn't have a corresponding association.");
            }

            if (entityType == FromEntitySet.EntityType)
            {
                foreach (XmlElement key in _associationElement.SelectNodes("ssdl:ReferentialConstraint/ssdl:Dependent/ssdl:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(oldName) + "]", NSM))
                {
                    key.SetAttribute("Name", newName);
                }
            }
            else if (entityType == ToEntitySet.EntityType)
            {
                foreach (XmlElement key in _associationElement.SelectNodes("ssdl:ReferentialConstraint/ssdl:Principal/ssdl:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(oldName) + "]", NSM))
                {
                    key.SetAttribute("Name", newName);
                }
            }
            else
            {
                throw new ArgumentException("The entity type " + entityType.Name + " does not participate in the association " + this.Name);
            }
        }
        /// <summary>
        /// Removes a pair of key members from the association.
        /// </summary>
        /// <param name="key">Pair of store member properties to remove from the association.</param>
        public void RemoveKey(Tuple <StoreMemberProperty, StoreMemberProperty> key)
        {
            if (_keys.Contains(key))
            {
                _keys.Remove(key);
            }

            if (_associationElement != null)
            {
                XmlElement fromKeyElement = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Dependent/ssdl:PropertyRef[@Name=" + XmlHelpers.XPathLiteral(key.Item1.Name) + "]", NSM);
                if (fromKeyElement != null)
                {
                    if (fromKeyElement.ParentNode != null)
                    {
                        fromKeyElement.ParentNode.RemoveChild(fromKeyElement);
                    }
                }

                XmlElement toKeyElement = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Principal/ssdl:PropertyRef", NSM);
                if (toKeyElement != null)
                {
                    if (toKeyElement.ParentNode != null)
                    {
                        toKeyElement.ParentNode.RemoveChild(toKeyElement);
                    }
                }
            }
        }