internal StoreMemberProperty(EDMXFile parentFile, StoreEntityType storeEntityType, string name, int ordinal, XmlElement parentTypeElement)
            : base(parentFile)
        {
            _parentEntityType          = storeEntityType;
            _parentEntityType.Removed += new EventHandler(ParentEntityType_Removed);

            _propertyElement = EDMXDocument.CreateElement("Property", NameSpaceURIssdl);
            if (ordinal > 0)
            {
                XmlNodeList propertyNodes = parentTypeElement.SelectNodes("ssdl:Property", NSM);
                if (propertyNodes.Count >= ordinal)
                {
                    parentTypeElement.InsertAfter(_propertyElement, propertyNodes[ordinal - 1]);
                }
                else
                {
                    parentTypeElement.AppendChild(_propertyElement);
                }
            }
            else
            {
                parentTypeElement.AppendChild(_propertyElement);
            }

            this.Name = name;
        }
        internal NavigationProperty(EDMXFile parentFile, ModelEntityType modelEntityType, string name, ModelAssociationSet modelAssociationSet, XmlElement entityTypeElement, string fromRoleName, string toRoleName)
            : base(parentFile)
        {
            _modelEntityType = modelEntityType;

            _propertyElement = EDMXDocument.CreateElement("NavigationProperty", NameSpaceURIcsdl);
            _propertyElement.SetAttribute("Relationship", modelAssociationSet.FullName);

            if (string.IsNullOrEmpty(fromRoleName) || string.IsNullOrEmpty(toRoleName))
            {
                if (modelAssociationSet.FromEntityType == _modelEntityType)
                {
                    fromRoleName = modelAssociationSet.FromRoleName;
                    toRoleName   = modelAssociationSet.ToRoleName;
                }
                else
                {
                    fromRoleName = modelAssociationSet.ToRoleName;
                    toRoleName   = modelAssociationSet.FromRoleName;
                }
            }

            _propertyElement.SetAttribute("FromRole", fromRoleName);
            _propertyElement.SetAttribute("ToRole", toRoleName);

            entityTypeElement.AppendChild(_propertyElement);

            Name = name;
        }
        private XmlElement CreateAssociation(string name)
        {
            XmlElement assocParent = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:StorageModels/ssdl:Schema", NSM);
            XmlElement assoc       = null;

            if (assocParent != null)
            {
                assoc = EDMXDocument.CreateElement("Association", NameSpaceURIssdl);
                assocParent.AppendChild(assoc);

                XmlElement toEndPoint = EDMXDocument.CreateElement("End", NameSpaceURIssdl);
                toEndPoint.SetAttribute("Multiplicity", "1");
                XmlElement fromEndPoint = EDMXDocument.CreateElement("End", NameSpaceURIssdl);
                assoc.AppendChild(toEndPoint);
                fromEndPoint.SetAttribute("Multiplicity", "*");
                assoc.AppendChild(fromEndPoint);

                XmlElement constraint = EDMXDocument.CreateElement("ReferentialConstraint", NameSpaceURIssdl);
                assoc.AppendChild(constraint);

                XmlElement toKeysContainer = EDMXDocument.CreateElement("Principal", NameSpaceURIssdl);
                constraint.AppendChild(toKeysContainer);

                XmlElement fromKeysContainer = EDMXDocument.CreateElement("Dependent", NameSpaceURIssdl);
                constraint.AppendChild(fromKeysContainer);
            }
            return(assoc);
        }
Exemple #4
0
        internal StoreFunctionParameter(EDMXFile parentFile, StoreFunction storeFunction, string name, int ordinal, XmlElement parentTypeElement)
            : base(parentFile)
        {
            _parentFunction = storeFunction;

            _parameterElement = EDMXDocument.CreateElement("Parameter", NameSpaceURIssdl);
            if (ordinal > 0)
            {
                XmlNodeList propertyNodes = parentTypeElement.SelectNodes("ssdl:Parameter", NSM);
                if (propertyNodes.Count >= ordinal)
                {
                    parentTypeElement.InsertAfter(_parameterElement, propertyNodes[ordinal - 1]);
                }
                else
                {
                    parentTypeElement.AppendChild(_parameterElement);
                }
            }
            else
            {
                parentTypeElement.AppendChild(_parameterElement);
            }

            this.Name = name;
        }
Exemple #5
0
        internal ModelMemberProperty(EDMXFile parentFile, ModelEntityType modelEntityType, string name, int ordinal, XmlElement entityTypeElement)
            : base(parentFile)
        {
            _modelEntityType          = modelEntityType;
            _modelEntityType.Removed += new EventHandler(ModelEntityType_Removed);

            _propertyElement = EDMXDocument.CreateElement("Property", NameSpaceURIcsdl);
            if (ordinal > 0)
            {
                XmlNodeList propertyNodes = entityTypeElement.SelectNodes("edm:Property", NSM);
                if (propertyNodes.Count >= ordinal)
                {
                    entityTypeElement.InsertAfter(_propertyElement, propertyNodes[ordinal - 1]);
                }
                else
                {
                    entityTypeElement.AppendChild(_propertyElement);
                }
            }
            else
            {
                entityTypeElement.AppendChild(_propertyElement);
            }

            Name = name;
        }
Exemple #6
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);
        }
        internal AssociationSetMapping(EDMXFile parentFile, XmlElement entityContainerMappingElement, CSMapping csMapping, string name, ModelAssociationSet modelAssocSet, StoreEntitySet storeEntitySet, StoreAssociationSet fromStoreAssocSet, StoreAssociationSet toStoreAssocSet)
            : base(parentFile)
        {
            _csMapping           = csMapping;
            _modelAssociationSet = modelAssocSet;

            //create mapping xml elements
            _asmElement = EDMXDocument.CreateElement("AssociationSetMapping", NameSpaceURImap);
            entityContainerMappingElement.AppendChild(_asmElement);

            XmlElement fromEndProp = EDMXDocument.CreateElement("EndProperty", NameSpaceURImap);

            fromEndProp.SetAttribute("Name", modelAssocSet.FromRoleName);
            _asmElement.AppendChild(fromEndProp);

            XmlElement toEndProp = EDMXDocument.CreateElement("EndProperty", NameSpaceURImap);

            toEndProp.SetAttribute("Name", modelAssocSet.ToRoleName);
            _asmElement.AppendChild(toEndProp);

            List <Tuple <ModelMemberProperty, StoreMemberProperty, string> > fromKeys = (
                from key in fromStoreAssocSet.Keys
                select new Tuple <ModelMemberProperty, StoreMemberProperty, string>(
                    key.Item2.ModelMembers.FirstOrDefault(mm => mm.EntityType == modelAssocSet.FromEntityType),
                    key.Item1,
                    key.Item2.Name
                    )
                ).ToList();

            foreach (var key in fromKeys)
            {
                XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                scalarProperty.SetAttribute("Name", (key.Item1 != null ? key.Item1.Name : key.Item3));
                scalarProperty.SetAttribute("ColumnName", key.Item2.Name);
                fromEndProp.AppendChild(scalarProperty);
            }

            List <Tuple <ModelMemberProperty, StoreMemberProperty, string> > toKeys =
                (
                    from key in toStoreAssocSet.Keys
                    select new Tuple <ModelMemberProperty, StoreMemberProperty, string>(
                        key.Item2.ModelMembers.FirstOrDefault(mm => mm.EntityType == modelAssocSet.ToEntityType),
                        key.Item1,
                        key.Item2.Name
                        )
                ).ToList();

            foreach (var key in toKeys)
            {
                XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                scalarProperty.SetAttribute("Name", (key.Item1 != null ? key.Item1.Name : key.Item3));
                scalarProperty.SetAttribute("ColumnName", key.Item2.Name);
                toEndProp.AppendChild(scalarProperty);
            }

            Name = name;
            StoreEntitySetName = storeEntitySet.Name;
            TypeName           = modelAssocSet.FullName;
        }
 /// <summary>
 /// Adds an xml comment to the entity type.
 /// </summary>
 /// <param name="commentText">Text contained in the comment.</param>
 public void AddComment(string commentText)
 {
     if (_entityTypeElement != null && _entityTypeElement.ParentNode != null)
     {
         XmlComment commentElement = EDMXDocument.CreateComment(commentText);
         _entityTypeElement.AppendChild(commentElement);
     }
 }
Exemple #9
0
        internal ModelMemberProperty(EDMXFile parentFile, ModelComplexType modelComplexType, string name, XmlElement entityTypeElement)
            : base(parentFile)
        {
            _modelComplexType          = modelComplexType;
            _modelComplexType.Removed += new EventHandler(ModelComplexType_Removed);

            _propertyElement = EDMXDocument.CreateElement("Property", NameSpaceURIcsdl);
            entityTypeElement.AppendChild(_propertyElement);

            Name = name;
        }
Exemple #10
0
        internal ModelEntitySet(EDMXFile parentFile, ConceptualModel conceptualModel, string name) : base(parentFile)
        {
            _conceptualModel = conceptualModel;

            //create and add the entity set element
            XmlElement setContainer = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer", NSM);

            _entitySetElement = EDMXDocument.CreateElement("EntitySet", NameSpaceURIcsdl);
            setContainer.AppendChild(_entitySetElement);

            Name = name;
        }
        internal StoreEntityType(EDMXFile parentFile, StorageModel storageModel, string name) : base(parentFile)
        {
            _storageModel = storageModel;

            //create the entity type element
            XmlElement schemaContainer = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:StorageModels/ssdl:Schema", NSM);

            _entityTypeElement = EDMXDocument.CreateElement("EntityType", NameSpaceURIssdl);
            schemaContainer.AppendChild(_entityTypeElement);

            Name = name;
        }
        internal StoreEntitySet(EDMXFile parentFile, StorageModel storageModel, string name) : base(parentFile)
        {
            _storageModel = storageModel;

            //create and add the entity set element
            XmlElement setContainer = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:StorageModels/ssdl:Schema/ssdl:EntityContainer", NSM);

            _entitySetElement = EDMXDocument.CreateElement("EntitySet", NameSpaceURIssdl);
            setContainer.AppendChild(_entitySetElement);

            this.Name = name;
        }
Exemple #13
0
        internal StoreFunction(EDMXFile ParentFile, StorageModel storageModel, string name)
            : base(ParentFile)
        {
            _storageModel = storageModel;

            //create and add the function element
            XmlElement schemaContainer = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:StorageModels/ssdl:Schema", NSM);

            _functionElement = EDMXDocument.CreateElement("Function", NameSpaceURIssdl);
            schemaContainer.AppendChild(_functionElement);

            this.Name = name;
        }
Exemple #14
0
        internal ModelComplexType(EDMXFile parentFile, ConceptualModel conceptualModel, string name)
            : base(parentFile)
        {
            _conceptualModel = conceptualModel;

            //create the entity type element
            XmlElement schemaContainer = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:ConceptualModels/edm:Schema", NSM);

            _complexTypeElement = EDMXDocument.CreateElement("ComplexType", NameSpaceURIcsdl);
            schemaContainer.AppendChild(_complexTypeElement);

            Name = name;
        }
Exemple #15
0
        private XmlElement CreateAssociationSet()
        {
            XmlElement assocSetParent = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer", NSM);
            XmlElement assocSet       = null;

            if (assocSetParent != null)
            {
                assocSet = EDMXDocument.CreateElement("AssociationSet", NameSpaceURIcsdl);
                assocSetParent.AppendChild(assocSet);
                XmlElement toEndPoint   = EDMXDocument.CreateElement("End", NameSpaceURIcsdl);
                XmlElement fromEndPoint = EDMXDocument.CreateElement("End", NameSpaceURIcsdl);
                assocSet.AppendChild(toEndPoint);
                assocSet.AppendChild(fromEndPoint);
            }
            return(assocSet);
        }
Exemple #16
0
        private XmlElement AddRefConstraint(XmlElement associationElement)
        {
            XmlElement refConstraint = EDMXDocument.CreateElement("ReferentialConstraint", NameSpaceURIcsdl);

            associationElement.AppendChild(refConstraint);

            XmlElement toKeysContainer = EDMXDocument.CreateElement("Principal", NameSpaceURIcsdl);

            refConstraint.AppendChild(toKeysContainer);

            XmlElement fromKeysContainer = EDMXDocument.CreateElement("Dependent", NameSpaceURIcsdl);

            refConstraint.AppendChild(fromKeysContainer);

            return(refConstraint);
        }
Exemple #17
0
        internal EntitySetMapping(EDMXFile parentFile, XmlElement entityContainerMappingElement, CSMapping csMapping, ModelEntitySet modelEntitySet, StoreEntitySet[] storeEntitySets)
            : base(parentFile)
        {
            _csMapping = csMapping;

            //create mapping xml elements
            _esmElement = EDMXDocument.CreateElement("EntitySetMapping", NameSpaceURImap);
            _esmElement.SetAttribute("Name", modelEntitySet.Name);
            entityContainerMappingElement.AppendChild(_esmElement);

            //entity type mapping
            XmlElement entityTypeMapping = EDMXDocument.CreateElement("EntityTypeMapping", NameSpaceURImap);

            if ((modelEntitySet.EntityType.HasBaseType || modelEntitySet.EntityType.HasSubTypes) && modelEntitySet.InheritanceStrategy != EDMXInheritanceStrategyEnum.TPC)
            {
                entityTypeMapping.SetAttribute("TypeName", "IsTypeOf(" + modelEntitySet.EntityType.FullName + ")");
            }
            else
            {
                entityTypeMapping.SetAttribute("TypeName", modelEntitySet.EntityType.FullName);
            }
            _esmElement.AppendChild(entityTypeMapping);

            foreach (StoreEntitySet ses in storeEntitySets)
            {
                //mapping fragment wrapper
                XmlElement mappingFragment = EDMXDocument.CreateElement("MappingFragment", NameSpaceURImap);
                mappingFragment.SetAttribute("StoreEntitySet", ses.Name);
                entityTypeMapping.AppendChild(mappingFragment);
            }

            //keep references to entity sets...
            _modelEntitySet = modelEntitySet;
            _storeEntitySets.AddRange(storeEntitySets);

            //let the involved entity sets know about the change in mappings...
            modelEntitySet.CSMappingsUpdated();
            storeEntitySets.ToList().ForEach(es => es.CSMappingsUpdated());

            //hook up remove events
            _modelEntitySet.Removed += new EventHandler(modelEntitySet_Removed);
            foreach (StoreEntitySet ses in _storeEntitySets)
            {
                ses.Removed += new EventHandler(storeEntitySet_Removed);
            }
        }
        internal StorageModel(EDMXFile parentFile) : base(parentFile)
        {
            _storageModelElement = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:StorageModels", NSM);
            _schemaElement       = (XmlElement)_storageModelElement.SelectSingleNode("ssdl:Schema", NSM);

            if (_schemaElement == null)
            {
                _schemaElement = EDMXDocument.CreateElement("Schema", NameSpaceURIssdl);
                _storageModelElement.AppendChild(_schemaElement);
            }

            _entityContainerElement = (XmlElement)_schemaElement.SelectSingleNode("ssdl:EntityContainer", NSM);
            if (_entityContainerElement == null)
            {
                _entityContainerElement = EDMXDocument.CreateElement("EntityContainer", NameSpaceURIssdl);
                _schemaElement.AppendChild(_entityContainerElement);
            }
        }
Exemple #19
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 #20
0
        internal CSMapping(EDMXFile parentFile)
            : base(parentFile)
        {
            _mappingsElement = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:Mappings", NSM);
            _mappingElement  = (XmlElement)_mappingsElement.SelectSingleNode("map:Mapping", NSM);
            if (_mappingElement == null)
            {
                _mappingElement = EDMXDocument.CreateElement("Mapping", NameSpaceURImap);
                _mappingElement.SetAttribute("Space", "C-S");
                _mappingsElement.AppendChild(_mappingElement);
            }

            _entityContainerMapping = (XmlElement)_mappingElement.SelectSingleNode("map:EntityContainerMapping", NSM);
            if (_entityContainerMapping == null)
            {
                _entityContainerMapping = EDMXDocument.CreateElement("EntityContainerMapping", NameSpaceURImap);
                _mappingElement.AppendChild(_entityContainerMapping);
            }
        }
        /// <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);
        }
Exemple #22
0
        private XmlElement CreateAssociation(string name, bool manyToMany)
        {
            XmlElement assocParent = (XmlElement)EDMXDocument.DocumentElement.SelectSingleNode("edmx:Runtime/edmx:ConceptualModels/edm:Schema", NSM);
            XmlElement assoc       = null;

            if (assocParent != null)
            {
                assoc = EDMXDocument.CreateElement("Association", NameSpaceURIcsdl);
                assocParent.AppendChild(assoc);

                XmlElement toEndPoint   = EDMXDocument.CreateElement("End", NameSpaceURIcsdl);
                XmlElement fromEndPoint = EDMXDocument.CreateElement("End", NameSpaceURIcsdl);
                assoc.AppendChild(toEndPoint);
                assoc.AppendChild(fromEndPoint);

                if (manyToMany == false)
                {
                    AddRefConstraint(assoc);
                }
            }
            return(assoc);
        }
Exemple #23
0
        /// <summary>
        /// Adds a key pair to the association. This should involve a member in the principal entity type and the corresponding member in the dependent entity type
        /// </summary>
        /// <param name="fromKey">A member of the principal entity type</param>
        /// <param name="toKey">A member of the dependent entity type</param>
        /// <returns></returns>
        public Tuple <ModelMemberProperty, ModelMemberProperty> AddKey(ModelMemberProperty fromKey, ModelMemberProperty toKey)
        {
            if (fromKey == null)
            {
                throw new ArgumentNullException("fromKey");
            }
            if (toKey == null)
            {
                throw new ArgumentNullException("toKey");
            }

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

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

            _keys.Add(newKey);

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

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

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

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

            return(newKey);
        }
        private void AddSaveComment()
        {
            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            string version = asm.GetName().Version.ToString();

            //remove old update comments
            foreach (XmlComment xc in EDMXDocument.DocumentElement.ChildNodes.OfType <XmlComment>().ToList())
            {
                string value = xc.Value;
                if (!string.IsNullOrEmpty(value))
                {
                    if (value.Trim().StartsWith("Updated by Huagati EDMX Tools"))
                    {
                        xc.ParentNode.RemoveChild(xc);
                    }
                }
            }

            //add a new update comment
            XmlComment comment = EDMXDocument.CreateComment("Updated by Huagati EDMX Tools version " + version + " on " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new System.Globalization.CultureInfo("en-us").DateTimeFormat));

            EDMXDocument.DocumentElement.InsertBefore(comment, EDMXDocument.DocumentElement.ChildNodes[0]);
        }
Exemple #25
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 #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.");
            }
        }