Esempio n. 1
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);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Adds a scalar member to the entity type.
 /// </summary>
 /// <param name="name">Scalar member name.</param>
 /// <param name="dataType">Data type name</param>
 /// <param name="ordinal">Ordinal position of the member within the type. (zero-based)</param>
 /// <returns>A StoreMemberProperty object.</returns>
 public StoreMemberProperty AddMember(string name, string dataType, int ordinal)
 {
     try
     {
         if (!MemberProperties.Where(mp => mp.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).Any())
         {
             StoreMemberProperty mp = new StoreMemberProperty(base.ParentFile, this, name, ordinal, _entityTypeElement);
             mp.DataType = dataType;
             _memberProperties.Add(name, mp);
             mp.NameChanged += new EventHandler <NameChangeArgs>(prop_NameChanged);
             mp.Removed     += new EventHandler(prop_Removed);
             return(mp);
         }
         else
         {
             throw new ArgumentException("A member property with the name " + name + " already exist in the type " + this.Name);
         }
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
Esempio n. 3
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);
        }
        void fromKey_Removed(object sender, EventArgs e)
        {
            StoreMemberProperty storeMemberProperty = (StoreMemberProperty)sender;

            _keys.RemoveAll(k => k.Item1 == storeMemberProperty);
            RemoveKeyFrom(storeMemberProperty);
        }
        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);
            }
        }
Esempio n. 6
0
        void smp_Removed(object sender, EventArgs e)
        {
            StoreMemberProperty storeMemberProperty = (StoreMemberProperty)sender;

            _memberMappings.RemoveAll(mmp => mmp.Item1 == storeMemberProperty);
            RemoveMemberMapping(storeMemberProperty);
        }
        void toKey_Removed(object sender, EventArgs e)
        {
            StoreMemberProperty storeMemberProperty = (StoreMemberProperty)sender;

            _keys.RemoveAll(k => k.Item2 == storeMemberProperty);
            RemoveKeyTo(storeMemberProperty);
        }
 /// <summary>
 /// Removes a key pair based on the 'From'-side key
 /// </summary>
 /// <param name="storeMemberProperty">From-key to remove the key pair for</param>
 public void RemoveKeyFrom(StoreMemberProperty storeMemberProperty)
 {
     foreach (Tuple <StoreMemberProperty, StoreMemberProperty> key in Keys.Where(k => k.Item1 == storeMemberProperty))
     {
         RemoveKey(key);
     }
 }
        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);
        }
 /// <summary>
 /// Copies attributes from another store member property.
 /// </summary>
 /// <param name="fromMember">Member to copy the attributes from.</param>
 public void CopyAttributes(StoreMemberProperty fromMember)
 {
     this.DataType = fromMember.DataType;
     this.Nullable = fromMember.Nullable;
     if (!string.IsNullOrEmpty(fromMember.Collation))
     {
         this.Collation = fromMember.Collation;
     }
     if (!string.IsNullOrEmpty(fromMember.DefaultValue))
     {
         this.DefaultValue = fromMember.DefaultValue;
     }
     if (this.MaxLengthApplies)
     {
         this.MaxLength   = fromMember.MaxLength;
         this.FixedLength = fromMember.FixedLength;
     }
     if (this.PrecisionScaleApplies)
     {
         this.Precision = fromMember.Precision;
         this.Scale     = fromMember.Scale;
     }
     if (!string.IsNullOrEmpty(fromMember.ShortDescription))
     {
         this.ShortDescription = fromMember.ShortDescription;
     }
     if (!string.IsNullOrEmpty(fromMember.LongDescription))
     {
         this.LongDescription = fromMember.LongDescription;
     }
 }
Esempio n. 11
0
 internal CSDLType(StoreMemberProperty storeMemberProperty)
 {
     this.Name = storeMemberProperty.Name;
     bool isUnicode = false;
     this.CLRTypeName = CSDLTypeNameFromSSDLTypeName(storeMemberProperty.DataType, out isUnicode);
     this.IsUnicode = isUnicode;
     this.MaxLength = storeMemberProperty.MaxLength;
     this.Nullable = storeMemberProperty.Nullable;
     this.Precision = storeMemberProperty.Precision;
     this.Scale = storeMemberProperty.Scale;
     this.FixedLength = storeMemberProperty.FixedLength;
     this.CompareIsUnicode = true;
 }
Esempio n. 12
0
        internal CSDLType(StoreMemberProperty storeMemberProperty)
        {
            this.Name = storeMemberProperty.Name;
            bool isUnicode = false;

            this.CLRTypeName      = CSDLTypeNameFromSSDLTypeName(storeMemberProperty.DataType, out isUnicode);
            this.IsUnicode        = isUnicode;
            this.MaxLength        = storeMemberProperty.MaxLength;
            this.Nullable         = storeMemberProperty.Nullable;
            this.Precision        = storeMemberProperty.Precision;
            this.Scale            = storeMemberProperty.Scale;
            this.FixedLength      = storeMemberProperty.FixedLength;
            this.CompareIsUnicode = true;
        }
Esempio n. 13
0
        /// <summary>
        /// Adds a mapping condition. Used for entity inheritance (e.g. TPH discriminators) or entity splitting.
        /// </summary>
        /// <param name="modelEntityType">Conceptual model entity type that this condition applies to.</param>
        /// <param name="discriminatorColumn">Store member that is used for the mapping condition.</param>
        /// <param name="discriminatorValue">Discriminator value that makes the mapping valid.</param>
        /// <returns>A MappingCondition object.</returns>
        public MappingCondition AddMappingCondition(ModelEntityType modelEntityType, StoreMemberProperty discriminatorColumn, string discriminatorValue)
        {
            MappingCondition mappingCondition = MappingConditions.FirstOrDefault(mc => mc.DiscriminatorColumn == discriminatorColumn && mc.ModelEntityType == modelEntityType);

            if (mappingCondition == null)
            {
                mappingCondition = new MappingCondition(this.ParentFile, this, _esmElement, modelEntityType, discriminatorColumn, discriminatorValue);
                if (_mappingConditions != null)
                {
                    _mappingConditions.Add(mappingCondition);
                    mappingCondition.Removed += new EventHandler(mappingCondition_Removed);
                }
            }
            return(mappingCondition);
        }
Esempio n. 14
0
        internal MappingCondition(EDMXFile parentFile, HuagatiEDMXTools.EntitySetMapping entitySetMapping, XmlElement conditionElement) : base(parentFile)
        {
            _entitySetMapping = entitySetMapping;

            _mappingCondition  = conditionElement;
            _mappingFragment   = (XmlElement)_mappingCondition.ParentNode;
            _entityTypeMapping = (XmlElement)_mappingFragment.ParentNode;

            string entityTypeName = EDMXUtils.StripTypeOf(_entityTypeMapping.GetAttribute("TypeName"));

            _modelEntityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase) || et.AliasName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase));

            if (_modelEntityType != null)
            {
                string columnName = _mappingCondition.GetAttribute("ColumnName");
                _discriminatorColumn = EntitySetMapping.StoreEntitySetsFor(_modelEntityType).SelectMany(c => c.EntityType.MemberProperties).FirstOrDefault(mp => mp.Name.Equals(columnName, StringComparison.InvariantCultureIgnoreCase));
            }
        }
        /// <summary>
        /// Adds a key pair to the association.
        /// </summary>
        /// <param name="fromKey">Dependent (foreign key owner table) key member.</param>
        /// <param name="toKey">Principal (foreign key referenced table) key member.</param>
        /// <returns></returns>
        public Tuple <StoreMemberProperty, StoreMemberProperty> AddKey(StoreMemberProperty fromKey, StoreMemberProperty toKey)
        {
            if (fromKey == null)
            {
                throw new ArgumentNullException("fromKey");
            }
            if (toKey == null)
            {
                throw new ArgumentNullException("toKey");
            }
            if (_associationElement == null)
            {
                throw new InvalidOperationException("The association set doesn't have a corresponding association.");
            }

            Tuple <StoreMemberProperty, StoreMemberProperty> newKey = new Tuple <StoreMemberProperty, StoreMemberProperty>(fromKey, toKey);

            fromKey.Removed += new EventHandler(fromKey_Removed);
            toKey.Removed   += new EventHandler(toKey_Removed);

            _keys.Add(newKey);

            XmlElement fromKeyContainer = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Dependent", NSM);

            if (fromKeyContainer != null)
            {
                XmlElement fromKeyElement = EDMXDocument.CreateElement("PropertyRef", NameSpaceURIssdl);
                fromKeyElement.SetAttribute("Name", fromKey.Name);
                fromKeyContainer.AppendChild(fromKeyElement);
            }

            XmlElement toKeyContainer = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Principal", NSM);

            if (toKeyContainer != null)
            {
                XmlElement toKeyElement = EDMXDocument.CreateElement("PropertyRef", NameSpaceURIssdl);
                toKeyElement.SetAttribute("Name", toKey.Name);
                toKeyContainer.AppendChild(toKeyElement);
            }

            return(newKey);
        }
 /// <summary>
 /// Retrieves an existing scalar member to the entity type, or creates a new one if it doesn't exist.
 /// </summary>
 /// <param name="name">Scalar member name.</param>
 /// <param name="dataType">Data type name</param>
 /// <param name="ordinal">Ordinal position of the member within the type. (zero-based)</param>
 /// <returns>A StoreMemberProperty object.</returns>
 public StoreMemberProperty GetOrCreateMember(string name, string dataType, int ordinal)
 {
     try
     {
         StoreMemberProperty storeMemberProperty = MemberProperties.FirstOrDefault(mp => mp.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
         if (storeMemberProperty == null)
         {
             storeMemberProperty = AddMember(name, dataType, ordinal);
         }
         return(storeMemberProperty);
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
        /// <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.");
            }
        }
 /// <summary>
 /// Adds a mapping condition. Used for entity inheritance (e.g. TPH discriminators) or entity splitting.
 /// </summary>
 /// <param name="modelEntityType">Conceptual model entity type that this condition applies to.</param>
 /// <param name="discriminatorColumn">Store member that is used for the mapping condition.</param>
 /// <param name="discriminatorValue">Discriminator value that makes the mapping valid.</param>
 /// <returns>A MappingCondition object.</returns>
 public MappingCondition AddMappingCondition(ModelEntityType modelEntityType, StoreMemberProperty discriminatorColumn, string discriminatorValue)
 {
     MappingCondition mappingCondition = MappingConditions.FirstOrDefault(mc => mc.DiscriminatorColumn == discriminatorColumn && mc.ModelEntityType == modelEntityType);
     if (mappingCondition == null)
     {
         mappingCondition = new MappingCondition(this.ParentFile, this, _esmElement, modelEntityType, discriminatorColumn, discriminatorValue);
         if (_mappingConditions != null)
         {
             _mappingConditions.Add(mappingCondition);
             mappingCondition.Removed += new EventHandler(mappingCondition_Removed);
         }
     }
     return mappingCondition;
 }
 /// <summary>
 /// Adds a member mapping from a conceptual model scalar member to a storage model scalar member.
 /// </summary>
 /// <param name="modelMemberProperty">Conceptual model scalar member to map</param>
 /// <param name="storeMemberProperty">Storage model scalar member to map to</param>
 public void AddMemberMapping(ModelMemberProperty modelMemberProperty, StoreMemberProperty storeMemberProperty)
 {
     AddMemberMapping(modelMemberProperty, storeMemberProperty, modelMemberProperty.EntityType);
 }
        internal MappingCondition(EDMXFile parentFile, HuagatiEDMXTools.EntitySetMapping entitySetMapping, XmlElement conditionElement)
            : base(parentFile)
        {
            _entitySetMapping = entitySetMapping;

            _mappingCondition = conditionElement;
            _mappingFragment = (XmlElement)_mappingCondition.ParentNode;
            _entityTypeMapping = (XmlElement)_mappingFragment.ParentNode;

            string entityTypeName = EDMXUtils.StripTypeOf(_entityTypeMapping.GetAttribute("TypeName"));
            _modelEntityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase) || et.AliasName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase));

            if (_modelEntityType != null)
            {
                string columnName = _mappingCondition.GetAttribute("ColumnName");
                _discriminatorColumn = EntitySetMapping.StoreEntitySetsFor(_modelEntityType).SelectMany(c => c.EntityType.MemberProperties).FirstOrDefault(mp => mp.Name.Equals(columnName, StringComparison.InvariantCultureIgnoreCase));
            }
        }
        /// <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);
                }
            }
        }
        private void EnumerateKeys()
        {
            try
            {
                if (_associationElement == null)
                {
                    throw new InvalidOperationException("The association set doesn't have a corresponding association.");
                }

                //get hold of key propertyrefs
                XmlNodeList fromKeys = _associationElement.SelectNodes("ssdl:ReferentialConstraint/ssdl:Dependent/ssdl:PropertyRef", NSM);
                XmlNodeList toKeys   = _associationElement.SelectNodes("ssdl:ReferentialConstraint/ssdl:Principal/ssdl:PropertyRef", NSM);

                //number of keys?
                int keyCount = Math.Max(fromKeys.Count, toKeys.Count);
                int keyNo    = 0;
                while (keyNo < keyCount)
                {
                    //get the from entity type member
                    StoreMemberProperty fromKey = null;
                    if (fromKeys.Count > keyNo)
                    {
                        string fromName = ((XmlElement)fromKeys[keyNo]).GetAttribute("Name");
                        fromKey = FromEntitySet.EntityType.MemberProperties.FirstOrDefault(mp => mp.Name.Equals(fromName, StringComparison.InvariantCultureIgnoreCase));
                    }

                    //get the to entity type member
                    StoreMemberProperty toKey = null;
                    if (toKeys.Count > keyNo)
                    {
                        string toName = ((XmlElement)toKeys[keyNo]).GetAttribute("Name");
                        toKey = ToEntitySet.EntityType.MemberProperties.FirstOrDefault(mp => mp.Name.Equals(toName, StringComparison.InvariantCultureIgnoreCase));
                    }

                    Tuple <StoreMemberProperty, StoreMemberProperty> key = null;
                    if (!_keys.Any(k => k.Item1.Equals(fromKey) && k.Item2.Equals(toKey)))
                    {
                        key = new Tuple <StoreMemberProperty, StoreMemberProperty>(fromKey, toKey);
                        _keys.Add(key);
                    }
                    else
                    {
                        key = _keys.FirstOrDefault(k => k.Item1.Equals(fromKey) && k.Item2.Equals(toKey));
                    }

                    if (fromKey != null)
                    {
                        fromKey.Removed += new EventHandler(fromKey_Removed);
                    }
                    if (toKey != null)
                    {
                        toKey.Removed += new EventHandler(toKey_Removed);
                    }

                    keyNo++;
                }

                _keysEnumerated = true;
            }
            catch (Exception ex)
            {
                try
                {
                    ExceptionTools.AddExceptionData(ex, this);
                }
                catch { }
                throw;
            }
        }
Esempio n. 23
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);
                }
            }
        }
        /// <summary>
        /// Adds a key pair to the association.
        /// </summary>
        /// <param name="fromKey">Dependent (foreign key owner table) key member.</param>
        /// <param name="toKey">Principal (foreign key referenced table) key member.</param>
        /// <returns></returns>
        public Tuple<StoreMemberProperty, StoreMemberProperty> AddKey(StoreMemberProperty fromKey, StoreMemberProperty toKey)
        {
            if (fromKey == null) { throw new ArgumentNullException("fromKey"); }
            if (toKey == null) { throw new ArgumentNullException("toKey"); }
            if (_associationElement == null) { throw new InvalidOperationException("The association set doesn't have a corresponding association."); }

            Tuple<StoreMemberProperty, StoreMemberProperty> newKey = new Tuple<StoreMemberProperty, StoreMemberProperty>(fromKey, toKey);

            fromKey.Removed += new EventHandler(fromKey_Removed);
            toKey.Removed += new EventHandler(toKey_Removed);

            _keys.Add(newKey);

            XmlElement fromKeyContainer = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Dependent", NSM);
            if (fromKeyContainer != null)
            {
                XmlElement fromKeyElement = EDMXDocument.CreateElement("PropertyRef", NameSpaceURIssdl);
                fromKeyElement.SetAttribute("Name", fromKey.Name);
                fromKeyContainer.AppendChild(fromKeyElement);
            }

            XmlElement toKeyContainer = (XmlElement)_associationElement.SelectSingleNode("ssdl:ReferentialConstraint/ssdl:Principal", NSM);
            if (toKeyContainer != null)
            {
                XmlElement toKeyElement = EDMXDocument.CreateElement("PropertyRef", NameSpaceURIssdl);
                toKeyElement.SetAttribute("Name", toKey.Name);
                toKeyContainer.AppendChild(toKeyElement);
            }

            return newKey;
        }
Esempio n. 25
0
        private void EnumerateMemberMappings()
        {
            foreach (XmlElement sp in _esmElement.SelectNodes("map:EntityTypeMapping/map:MappingFragment/map:ScalarProperty", NSM))
            {
                string modelPropertyName = sp.GetAttribute("Name");
                string entityTypeName    = EDMXUtils.StripTypeOf(((XmlElement)sp.ParentNode.ParentNode).GetAttribute("TypeName"));

                ModelEntityType     entityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName == entityTypeName || et.AliasName == entityTypeName);
                ModelMemberProperty mmp        = entityType.MemberProperties.FirstOrDefault(mp => mp.Name == modelPropertyName);

                if (mmp != null)
                {
                    string         storeEntitySetName = ((XmlElement)sp.ParentNode).GetAttribute("StoreEntitySet");
                    StoreEntitySet ses = ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));

                    if (ses != null)
                    {
                        string storePropertyName = sp.GetAttribute("ColumnName");
                        StoreMemberProperty smp  = ses.EntityType.MemberProperties.FirstOrDefault(mp => mp.Name.Equals(storePropertyName, StringComparison.InvariantCultureIgnoreCase));

                        if (smp != null)
                        {
                            _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(smp, mmp, entityType));

                            smp.Removed += new EventHandler(smp_Removed);
                            mmp.Removed += new EventHandler(mmp_Removed);
                        }
                    }
                }
            }
            foreach (XmlElement sp in _esmElement.SelectNodes("map:EntityTypeMapping/map:MappingFragment/map:ComplexProperty/map:ScalarProperty", NSM))
            {
                string modelPropertyName = sp.GetAttribute("Name");

                string           complexTypeName = EDMXUtils.StripTypeOf(((XmlElement)sp.ParentNode).GetAttribute("TypeName"));
                ModelComplexType complexType     = ParentFile.ConceptualModel.ComplexTypes.FirstOrDefault(ct => ct.FullName == complexTypeName || ct.AliasName == complexTypeName);

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

                ModelMemberProperty mmp = null;
                if (complexType != null)
                {
                    mmp = complexType.MemberProperties.FirstOrDefault(mp => mp.Name == modelPropertyName);

                    if (mmp != null)
                    {
                        string         storeEntitySetName = ((XmlElement)sp.ParentNode.ParentNode).GetAttribute("StoreEntitySet");
                        StoreEntitySet ses = ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));

                        if (ses != null)
                        {
                            string storePropertyName = sp.GetAttribute("ColumnName");
                            StoreMemberProperty smp  = ses.EntityType.MemberProperties.FirstOrDefault(mp => mp.Name.Equals(storePropertyName, StringComparison.InvariantCultureIgnoreCase));

                            if (smp != null)
                            {
                                _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(smp, mmp, entityType));

                                smp.Removed += new EventHandler(smp_Removed);
                                mmp.Removed += new EventHandler(mmp_Removed);
                            }
                        }
                    }
                }
            }
            _memberMappingsEnumerated = true;
        }
Esempio n. 26
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.");
            }
        }
Esempio n. 27
0
 /// <summary>
 /// Adds a member mapping from a conceptual model scalar member to a storage model scalar member.
 /// </summary>
 /// <param name="modelMemberProperty">Conceptual model scalar member to map</param>
 /// <param name="storeMemberProperty">Storage model scalar member to map to</param>
 public void AddMemberMapping(ModelMemberProperty modelMemberProperty, StoreMemberProperty storeMemberProperty)
 {
     AddMemberMapping(modelMemberProperty, storeMemberProperty, modelMemberProperty.EntityType);
 }
 /// <summary>
 /// Removes a key pair based on the 'To'-side key
 /// </summary>
 /// <param name="storeMemberProperty">To-key to remove the key pair for</param>
 public void RemoveKeyTo(StoreMemberProperty storeMemberProperty)
 {
     foreach (Tuple<StoreMemberProperty, StoreMemberProperty> key in Keys.Where(k => k.Item2 == storeMemberProperty))
     {
         RemoveKey(key);
     }
 }
        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 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);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Adds a scalar member to the entity type.
 /// </summary>
 /// <param name="name">Scalar member name.</param>
 /// <param name="dataType">Data type name</param>
 /// <param name="ordinal">Ordinal position of the member within the type. (zero-based)</param>
 /// <returns>A StoreMemberProperty object.</returns>
 public StoreMemberProperty AddMember(string name, string dataType, int ordinal)
 {
     try
     {
         if (!MemberProperties.Where(mp => mp.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).Any())
         {
             StoreMemberProperty mp = new StoreMemberProperty(base.ParentFile, this, name, ordinal, _entityTypeElement);
             mp.DataType = dataType;
             _memberProperties.Add(name, mp);
             mp.NameChanged += new EventHandler<NameChangeArgs>(prop_NameChanged);
             mp.Removed += new EventHandler(prop_Removed);
             return mp;
         }
         else
         {
             throw new ArgumentException("A member property with the name " + name + " already exist in the type " + this.Name);
         }
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
 /// <summary>
 /// Copies attributes from another store member property.
 /// </summary>
 /// <param name="fromMember">Member to copy the attributes from.</param>
 public void CopyAttributes(StoreMemberProperty fromMember)
 {
     this.DataType = fromMember.DataType;
     this.Nullable = fromMember.Nullable;
     if (!string.IsNullOrEmpty(fromMember.Collation))
     {
         this.Collation = fromMember.Collation;
     }
     if (!string.IsNullOrEmpty(fromMember.DefaultValue))
     {
         this.DefaultValue = fromMember.DefaultValue;
     }
     if (this.MaxLengthApplies)
     {
         this.MaxLength = fromMember.MaxLength;
         this.FixedLength = fromMember.FixedLength;
     }
     if (this.PrecisionScaleApplies)
     {
         this.Precision = fromMember.Precision;
         this.Scale = fromMember.Scale;
     }
     if (!string.IsNullOrEmpty(fromMember.ShortDescription))
     {
         this.ShortDescription = fromMember.ShortDescription;
     }
     if (!string.IsNullOrEmpty(fromMember.LongDescription))
     {
         this.LongDescription = fromMember.LongDescription;
     }
 }