Exemple #1
0
 /// <summary>
 /// Retrieves an existing entity type, or creates a new one if no matching entity type is found.
 /// </summary>
 /// <param name="entityTypeName">Entity type name.</param>
 /// <param name="baseType">Base type that this entity type will inherit from if creating a new type.</param>
 /// <returns>A ModelEntityType object.</returns>
 public ModelEntityType GetOrCreateEntityType(string entityTypeName, ModelEntityType baseType)
 {
     try
     {
         ModelEntityType modelEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(entityTypeName));
         if (modelEntityType == null)
         {
             modelEntityType = AddEntityType(entityTypeName, baseType);
         }
         return(modelEntityType);
     }
     catch (Exception ex)
     {
         try
         {
             if (!ex.Data.Contains("EDMXType"))
             {
                 ex.Data.Add("EDMXType", this.GetType().Name);
             }
             if (!ex.Data.Contains("EDMXObjectName"))
             {
                 ex.Data.Add("EDMXObjectName", this.ContainerName);
             }
         }
         catch { }
         throw;
     }
 }
        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 #3
0
 /// <summary>
 /// Creates and adds a new conceptual model entity type inheriting from an existing entity type.
 /// </summary>
 /// <param name="name">Entity type name for the new entity type.</param>
 /// <param name="baseType">Base type that this entity type inherits from.</param>
 /// <returns>A ModelEntityType instance corresponding to the new entity type.</returns>
 public ModelEntityType AddEntityType(string name, ModelEntityType baseType)
 {
     try
     {
         if (!EntityTypes.Any(et => et.Name == name) &&
             !ComplexTypes.Any(ct => ct.Name == name))
         {
             ModelEntityType et = new ModelEntityType(ParentFile, this, name, baseType);
             _modelEntityTypes.Add(name, et);
             et.NameChanged += new EventHandler <NameChangeArgs>(et_NameChanged);
             et.Removed     += new EventHandler(et_Removed);
             return(et);
         }
         else
         {
             throw new ArgumentException("A type with the name " + name + " already exist in the model.");
         }
     }
     catch (Exception ex)
     {
         try
         {
             if (!ex.Data.Contains("EDMXType"))
             {
                 ex.Data.Add("EDMXType", this.GetType().Name);
             }
             if (!ex.Data.Contains("EDMXObjectName"))
             {
                 ex.Data.Add("EDMXObjectName", this.ContainerName);
             }
         }
         catch { }
         throw;
     }
 }
        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 #5
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 #6
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 #7
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 #8
0
 internal ModelMemberProperty(EDMXFile parentFile, ModelEntityType modelEntityType, XmlElement memberPropertyElement)
     : base(parentFile)
 {
     _modelEntityType          = modelEntityType;
     _modelEntityType.Removed += new EventHandler(ModelEntityType_Removed);
     _propertyElement          = memberPropertyElement;
 }
        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;
        }
 internal ModelMemberProperty(EDMXFile parentFile, ModelEntityType modelEntityType, XmlElement memberPropertyElement)
     : base(parentFile)
 {
     _modelEntityType = modelEntityType;
     _modelEntityType.Removed += new EventHandler(ModelEntityType_Removed);
     _propertyElement = memberPropertyElement;
 }
        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;
        }
        /// <summary>
        /// Adds a store entityset to the model, based on an existing conceptual model entity set and entity type.
        /// </summary>
        /// <param name="modelEntitySet">Conceptual model entity set</param>
        /// <param name="modelEntityType">Conceptual model entity type</param>
        /// <param name="schemaName">Database schemaname for the new entity set.</param>
        /// <param name="tableName">Tablename for the new entityset.</param>
        /// <returns>A new StoreEntitySet object.</returns>
        public StoreEntitySet AddEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType, string schemaName, string tableName)
        {
            string entitySetName = null;

            if (!modelEntityType.HasBaseType)
            {
                entitySetName = modelEntitySet.Name;
            }
            else
            {
                entitySetName = modelEntityType.TopLevelBaseType.EntitySet.Name + "_" + modelEntityType.Name;// string.Join("_", modelEntityType.BaseTypes.Select(tn => tn.Name));
            }

            StoreEntitySet storeEntitySet = this.AddEntitySet(entitySetName);

            storeEntitySet.StoreType = StoreTypeEnum.Table;
            storeEntitySet.Schema    = schemaName;
            storeEntitySet.TableName = tableName;
            if (!string.IsNullOrEmpty(modelEntitySet.ShortDescription))
            {
                storeEntitySet.ShortDescription = modelEntitySet.ShortDescription;
            }
            if (!string.IsNullOrEmpty(modelEntitySet.LongDescription))
            {
                storeEntitySet.LongDescription = modelEntitySet.LongDescription;
            }
            return(storeEntitySet);
        }
Exemple #13
0
        internal EntityTypeShape(EDMXFile parentFile, Designer parentDesigner, ModelEntityType entityType)
            : base(parentFile)
        {
            _parentDesigner = parentDesigner;

            _shapeElement = parentDesigner.Document.CreateElement("EntityTypeShape", NameSpaceURIedmx);

            EntityTypeName = entityType.FullName;
            parentDesigner.DiagramElement.AppendChild(_shapeElement);
        }
        /// <summary>
        /// Retrieves an existing entitytype based on a conceptual model entittype, or creates a new one if a match can not be found.
        /// </summary>
        /// <param name="modelEntityType">Conceptual model entity type to match.</param>
        /// <returns>A StoreEntityType object.</returns>
        public StoreEntityType GetOrCreateEntityType(ModelEntityType modelEntityType)
        {
            StoreEntityType storeEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(modelEntityType.Name, StringComparison.InvariantCultureIgnoreCase));

            if (storeEntityType == null)
            {
                storeEntityType = AddEntityType(modelEntityType);
            }
            return(storeEntityType);
        }
        /// <summary>
        /// Adds a new entity type to the storage model, based on an existing conceptual model entity type.
        /// </summary>
        /// <param name="modelEntityType">Conceptual model entity type to use as a base.</param>
        /// <returns>A new StoreEntityType object.</returns>
        public StoreEntityType AddEntityType(ModelEntityType modelEntityType)
        {
            StoreEntityType storeEntityType = this.AddEntityType(modelEntityType.Name);

            if (!string.IsNullOrEmpty(modelEntityType.ShortDescription))
            {
                storeEntityType.ShortDescription = modelEntityType.ShortDescription;
            }
            if (!string.IsNullOrEmpty(modelEntityType.LongDescription))
            {
                storeEntityType.LongDescription = modelEntityType.LongDescription;
            }
            return(storeEntityType);
        }
        internal ModelEntityType(EDMXFile parentFile, ConceptualModel conceptualModel, string name, ModelEntityType baseType)
            : base(parentFile)
        {
            _conceptualModel = conceptualModel;

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

            BaseType = baseType;

            Name = name;
        }
Exemple #17
0
        /// <summary>
        /// Initiates auto-mapping of entity set members. The OnColumnMapping event will be fired for every suggested mapping between a conceptual model member property and a storage model member property.
        /// </summary>
        public void AutoMapMembers(ModelEntityType entityType)
        {
            //go through model entity set members and attempt to map to storage set(s) members.
            //  ...raise BeforeColumnMapping for each mapping suggestion, to give caller a chance to veto...
            foreach (ModelMemberProperty modelMemberProperty in entityType.MemberProperties)
            {
                List <StoreMemberProperty> mappingCandidates = _storeEntitySets.SelectMany(mp => mp.EntityType.MemberProperties.Where(mpn => mpn.Name.Equals(modelMemberProperty.Name))).ToList();
                if (mappingCandidates.Count > 0)
                {
                    //match(es) found
                    foreach (StoreMemberProperty storeMemberProperty in mappingCandidates)
                    {
                        AutoMapArgs args = new AutoMapArgs()
                        {
                            ModelMemberProperty = modelMemberProperty,
                            StoreMemberProperty = storeMemberProperty,
                            UseMapping          = true
                        };
                        if (OnColumnMapping != null)
                        {
                            OnColumnMapping(this, args);
                        }
                        if (args.UseMapping)
                        {
                            AddMemberMapping(modelMemberProperty, storeMemberProperty);
                        }
                    }
                }
                else
                {
                    //no match found, raise event to request mapping...
                    AutoMapArgs args = new AutoMapArgs()
                    {
                        ModelMemberProperty = modelMemberProperty,
                        StoreMemberProperty = null,
                        UseMapping          = false
                    };

                    if (OnColumnMapping != null)
                    {
                        OnColumnMapping(this, args);
                    }
                    if (args.UseMapping == true && args.StoreMemberProperty != null)
                    {
                        AddMemberMapping(modelMemberProperty, args.StoreMemberProperty);
                    }
                }
            }
        }
Exemple #18
0
        internal ModelEntityType(EDMXFile parentFile, ConceptualModel conceptualModel, string name, ModelEntityType baseType)
            : base(parentFile)
        {
            _conceptualModel = conceptualModel;

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

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

            BaseType = baseType;

            Name = name;
        }
Exemple #19
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);
        }
Exemple #20
0
 /// <summary>
 /// True if the specified entity type is a parent somewhere in the inheritance chain for this entity type.
 /// </summary>
 /// <param name="entityType">Entity type to look for in the inheritance chain.</param>
 /// <returns>True if found, false if not.</returns>
 public bool IsSubtypeOf(ModelEntityType entityType)
 {
     try
     {
         return(BaseTypes.Any(t => t == entityType));
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
Exemple #21
0
 /// <summary>
 /// True if the specified entity type inherits from this entity type.
 /// </summary>
 /// <param name="entityType">Entity type to look for in the subtypes of this entity.</param>
 /// <returns>True if found, false if not.</returns>
 public bool InheritsFrom(ModelEntityType entityType)
 {
     try
     {
         return(SubTypes.Any(t => t == entityType));
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
Exemple #22
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 #23
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 #24
0
        /// <summary>
        /// Adds an entity type shape to the diagram
        /// </summary>
        /// <param name="entityType">Model entity type to add to the diagram.</param>
        /// <returns>An EntityTypeShape object</returns>
        public EntityTypeShape AddEntityTypeShape(ModelEntityType entityType)
        {
            if (entityType == null)
            {
                throw new ArgumentNullException("entityType");
            }

            EntityTypeShape ets = EntityTypeShapes.FirstOrDefault(es => es.EntityType == entityType);

            if (ets == null)
            {
                ets          = new EntityTypeShape(ParentFile, this, entityType);
                ets.Removed += new EventHandler(ets_Removed);
                _entityTypeShapes.Add(ets.EntityTypeName, ets);
            }
            return(ets);
        }
Exemple #25
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));
            }
        }
Exemple #26
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);
                    }
                }
            }
        }
        /// <summary>
        /// Retrieves an existing entityset based on a conceptual model entityset/type, or creates a new one if a match can not be found.
        /// </summary>
        /// <param name="modelEntitySet">Conceptual model entity set to match.</param>
        /// <param name="modelEntityType">Conceptual model entity type to match.</param>
        /// <param name="schemaName">Database schemaname for the new entity set.</param>
        /// <param name="tableName">Tablename for the new entityset.</param>
        /// <returns>A StoreEntitySet object.</returns>
        public StoreEntitySet GetOrCreateEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType, string schemaName, string tableName)
        {
            string entitySetName = null;

            if (!modelEntityType.HasBaseType)
            {
                entitySetName = modelEntitySet.Name;
            }
            else
            {
                entitySetName = modelEntityType.TopLevelBaseType.EntitySet.Name + "_" + modelEntityType.Name;
            }

            StoreEntitySet storeEntitySet = EntitySets.FirstOrDefault(es => es.Name.Equals(entitySetName, StringComparison.InvariantCultureIgnoreCase));

            if (storeEntitySet == null)
            {
                storeEntitySet = AddEntitySet(modelEntitySet, modelEntityType, schemaName, tableName);
            }
            return(storeEntitySet);
        }
        internal ModelAssociationSet(EDMXFile parentFile, ConceptualModel conceptualModel, string name, ModelEntitySet fromES, ModelEntitySet toES, ModelEntityType fromET, ModelEntityType toET, MultiplicityTypeEnum fromMultiplicity, MultiplicityTypeEnum toMultiplicity, string fromNavProperty, string toNavProperty, List<Tuple<ModelMemberProperty, ModelMemberProperty>> keys)
            : base(parentFile)
        {
            _conceptualModel = conceptualModel;

            bool manyToMany = (fromMultiplicity == MultiplicityTypeEnum.Many && toMultiplicity == MultiplicityTypeEnum.Many);

            _associationSetElement = CreateAssociationSet();
            _associationElement = CreateAssociation(name, manyToMany);

            Name = name;

            //set from/to sets and multiplicity
            FromEntitySet = fromES;
            FromEntityType = fromET;
            FromMultiplicity = fromMultiplicity;
            ToEntitySet = toES;
            ToEntityType = toET;
            ToMultiplicity = toMultiplicity;

            //add navigation properties
            if (!string.IsNullOrEmpty(fromNavProperty))
            {
                fromET.AddNavigationMember(fromNavProperty, this, FromRoleName, ToRoleName);
            }
            if (!string.IsNullOrEmpty(toNavProperty))
            {
                toET.AddNavigationMember(toNavProperty, this, ToRoleName, FromRoleName);
            }

            if (keys != null)
            {
                foreach (Tuple<ModelMemberProperty, ModelMemberProperty> key in keys)
                {
                    AddKey(key.Item1, key.Item2);
                }
            }

            _keysEnumerated = true;
        }
Exemple #29
0
        internal ModelAssociationSet(EDMXFile parentFile, ConceptualModel conceptualModel, string name, ModelEntitySet fromES, ModelEntitySet toES, ModelEntityType fromET, ModelEntityType toET, MultiplicityTypeEnum fromMultiplicity, MultiplicityTypeEnum toMultiplicity, string fromNavProperty, string toNavProperty, List <Tuple <ModelMemberProperty, ModelMemberProperty> > keys)
            : base(parentFile)
        {
            _conceptualModel = conceptualModel;

            bool manyToMany = (fromMultiplicity == MultiplicityTypeEnum.Many && toMultiplicity == MultiplicityTypeEnum.Many);

            _associationSetElement = CreateAssociationSet();
            _associationElement    = CreateAssociation(name, manyToMany);

            Name = name;

            //set from/to sets and multiplicity
            FromEntitySet    = fromES;
            FromEntityType   = fromET;
            FromMultiplicity = fromMultiplicity;
            ToEntitySet      = toES;
            ToEntityType     = toET;
            ToMultiplicity   = toMultiplicity;

            //add navigation properties
            if (!string.IsNullOrEmpty(fromNavProperty))
            {
                fromET.AddNavigationMember(fromNavProperty, this, FromRoleName, ToRoleName);
            }
            if (!string.IsNullOrEmpty(toNavProperty))
            {
                toET.AddNavigationMember(toNavProperty, this, ToRoleName, FromRoleName);
            }

            if (keys != null)
            {
                foreach (Tuple <ModelMemberProperty, ModelMemberProperty> key in keys)
                {
                    AddKey(key.Item1, key.Item2);
                }
            }

            _keysEnumerated = true;
        }
Exemple #30
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 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>
        /// Initiates auto-mapping of entity set members. The OnColumnMapping event will be fired for every suggested mapping between a conceptual model member property and a storage model member property.
        /// </summary>
        public void AutoMapMembers(ModelEntityType entityType)
        {
            //go through model entity set members and attempt to map to storage set(s) members.
            //  ...raise BeforeColumnMapping for each mapping suggestion, to give caller a chance to veto...
            foreach (ModelMemberProperty modelMemberProperty in entityType.MemberProperties)
            {
                List<StoreMemberProperty> mappingCandidates = _storeEntitySets.SelectMany(mp => mp.EntityType.MemberProperties.Where(mpn => mpn.Name.Equals(modelMemberProperty.Name))).ToList();
                if (mappingCandidates.Count > 0)
                {
                    //match(es) found
                    foreach (StoreMemberProperty storeMemberProperty in mappingCandidates)
                    {
                        AutoMapArgs args = new AutoMapArgs()
                        {
                            ModelMemberProperty = modelMemberProperty,
                            StoreMemberProperty = storeMemberProperty,
                            UseMapping = true
                        };
                        if (OnColumnMapping != null)
                        {
                            OnColumnMapping(this, args);
                        }
                        if (args.UseMapping)
                        {
                            AddMemberMapping(modelMemberProperty, storeMemberProperty);
                        }
                    }
                }
                else
                {
                    //no match found, raise event to request mapping...
                    AutoMapArgs args = new AutoMapArgs()
                    {
                        ModelMemberProperty = modelMemberProperty,
                        StoreMemberProperty = null,
                        UseMapping = false
                    };

                    if (OnColumnMapping != null)
                    {
                        OnColumnMapping(this, args);
                    }
                    if (args.UseMapping == true && args.StoreMemberProperty != null)
                    {
                        AddMemberMapping(modelMemberProperty, args.StoreMemberProperty);
                    }
                }
            }
        }
        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 an entity type shape to the diagram
        /// </summary>
        /// <param name="entityType">Model entity type to add to the diagram.</param>
        /// <returns>An EntityTypeShape object</returns>
        public EntityTypeShape AddEntityTypeShape(ModelEntityType entityType)
        {
            if (entityType == null) { throw new ArgumentNullException("entityType"); }

            EntityTypeShape ets = EntityTypeShapes.FirstOrDefault(es => es.EntityType == entityType);
            if (ets == null)
            {
                ets = new EntityTypeShape(ParentFile, this, entityType);
                ets.Removed += new EventHandler(ets_Removed);
                _entityTypeShapes.Add(ets.EntityTypeName, ets);
            }
            return ets;
        }
 /// <summary>
 /// True if the specified entity type is a parent somewhere in the inheritance chain for this entity type.
 /// </summary>
 /// <param name="entityType">Entity type to look for in the inheritance chain.</param>
 /// <returns>True if found, false if not.</returns>
 public bool IsSubtypeOf(ModelEntityType entityType)
 {
     try
     {
         return BaseTypes.Any(t => t == entityType);
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
 /// <summary>
 /// Retrieves an existing entityset based on model entityset/type, or creates a new one if a match can not be found.
 /// </summary>
 /// <param name="modelEntitySet">Conceptual model entity set to match.</param>
 /// <param name="modelEntityType">Conceptual model entity type to match.</param>
 /// <returns>A StoreEntitySet object.</returns>
 public StoreEntitySet GetOrCreateEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType)
 {
     return(GetOrCreateEntitySet(modelEntitySet, modelEntityType, "dbo", modelEntitySet.Name));
 }
        /// <summary>
        /// Retrieves an existing entityset based on a conceptual model entityset/type, or creates a new one if a match can not be found.
        /// </summary>
        /// <param name="modelEntitySet">Conceptual model entity set to match.</param>
        /// <param name="modelEntityType">Conceptual model entity type to match.</param>
        /// <param name="schemaName">Database schemaname for the new entity set.</param>
        /// <param name="tableName">Tablename for the new entityset.</param>
        /// <returns>A StoreEntitySet object.</returns>
        public StoreEntitySet GetOrCreateEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType, string schemaName, string tableName)
        {
            string entitySetName = null;
            if (!modelEntityType.HasBaseType)
            {
                entitySetName = modelEntitySet.Name;
            }
            else
            {
                entitySetName = modelEntityType.TopLevelBaseType.EntitySet.Name + "_" + modelEntityType.Name;
            }

            StoreEntitySet storeEntitySet = EntitySets.FirstOrDefault(es => es.Name.Equals(entitySetName, StringComparison.InvariantCultureIgnoreCase));
            if (storeEntitySet == null)
            {
                storeEntitySet = AddEntitySet(modelEntitySet, modelEntityType, schemaName, tableName);
            }
            return storeEntitySet;
        }
 void entityType_Removed(object sender, EventArgs e)
 {
     _entityType = null;
 }
 /// <summary>
 /// Adds a store entityset to the model, based on an existing conceptual model entity set and entity type.
 /// </summary>
 /// <param name="modelEntitySet">Conceptual model entity set</param>
 /// <param name="modelEntityType">Conceptual model entity type</param>
 /// <returns>A new StoreEntitySet object.</returns>
 public StoreEntitySet AddEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType)
 {
     return AddEntitySet(modelEntitySet, modelEntityType, "dbo", modelEntitySet.Name);
 }
Exemple #40
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;
        }
Exemple #41
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.");
            }
        }
 /// <summary>
 /// Retrieves an existing entity type, or creates a new one if no matching entity type is found.
 /// </summary>
 /// <param name="entityTypeName">Entity type name.</param>
 /// <param name="baseType">Base type that this entity type will inherit from if creating a new type.</param>
 /// <returns>A ModelEntityType object.</returns>
 public ModelEntityType GetOrCreateEntityType(string entityTypeName, ModelEntityType baseType)
 {
     try
     {
         ModelEntityType modelEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(entityTypeName));
         if (modelEntityType == null)
         {
             modelEntityType = AddEntityType(entityTypeName, baseType);
         }
         return modelEntityType;
     }
     catch (Exception ex)
     {
         try
         {
             if (!ex.Data.Contains("EDMXType"))
             {
                 ex.Data.Add("EDMXType", this.GetType().Name);
             }
             if (!ex.Data.Contains("EDMXObjectName"))
             {
                 ex.Data.Add("EDMXObjectName", this.ContainerName);
             }
         }
         catch { }
         throw;
     }
 }
Exemple #43
0
 void entityType_Removed(object sender, EventArgs e)
 {
     _entityType = null;
 }
 /// <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;
             }
         }
     }
 }
        /// <summary>
        /// Adds a store entityset to the model, based on an existing conceptual model entity set and entity type.
        /// </summary>
        /// <param name="modelEntitySet">Conceptual model entity set</param>
        /// <param name="modelEntityType">Conceptual model entity type</param>
        /// <param name="schemaName">Database schemaname for the new entity set.</param>
        /// <param name="tableName">Tablename for the new entityset.</param>
        /// <returns>A new StoreEntitySet object.</returns>
        public StoreEntitySet AddEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType, string schemaName, string tableName)
        {
            string entitySetName = null;
            if (!modelEntityType.HasBaseType)
            {
                entitySetName = modelEntitySet.Name;
            }
            else
            {
                entitySetName = modelEntityType.TopLevelBaseType.EntitySet.Name + "_" + modelEntityType.Name;// string.Join("_", modelEntityType.BaseTypes.Select(tn => tn.Name));
            }

            StoreEntitySet storeEntitySet = this.AddEntitySet(entitySetName);
            storeEntitySet.StoreType = StoreTypeEnum.Table;
            storeEntitySet.Schema = schemaName;
            storeEntitySet.TableName = tableName;
            if (!string.IsNullOrEmpty(modelEntitySet.ShortDescription))
            {
                storeEntitySet.ShortDescription = modelEntitySet.ShortDescription;
            }
            if (!string.IsNullOrEmpty(modelEntitySet.LongDescription))
            {
                storeEntitySet.LongDescription = modelEntitySet.LongDescription;
            }
            return storeEntitySet;
        }
 /// <summary>
 /// Adds a new association set and association between two conceptual model entities.
 /// </summary>
 /// <param name="name">Name of the association and association set.</param>
 /// <param name="fromEntitySet">Entity set where the entity set originates from. For one-to-many associations, this is typically the many-side of the association.</param>
 /// <param name="toEntitySet">Entity set that the entity set references.</param>
 /// <param name="fromEntityType">Entity type that the association originates from. This should be the entity type or a descendant of the entity type for the entity set passed in the fromEntitySet parameter.</param>
 /// <param name="toEntityType">Entity type that the association references. This should be the entity type or a descendant of the entity type that the entity set passed in the toEntitySet parameter.</param>
 /// <param name="fromMultiplicity">Multiplicity for the from entity set.</param>
 /// <param name="toMultiplicity">Multiplicity for the to entity set.</param>
 /// <param name="fromNavigationProperty">Name for the conceptual model navigation property in the fromEntityType for the association.</param>
 /// <param name="toNavigationProperty">Name for the conceptual model navigation property in the toEntityType for the association.</param>
 /// <param name="keys">A list of the entity key pairs for the association. This is a list containing pairs of ModelMemberProperty instances from the From and To entity types.</param>
 /// <returns></returns>
 public ModelAssociationSet AddAssociation(string name, ModelEntitySet fromEntitySet, ModelEntitySet toEntitySet, ModelEntityType fromEntityType, ModelEntityType toEntityType, MultiplicityTypeEnum fromMultiplicity, MultiplicityTypeEnum toMultiplicity, string fromNavigationProperty, string toNavigationProperty, List<Tuple<ModelMemberProperty, ModelMemberProperty>> keys)
 {
     try
     {
         if (!AssociationSets.Where(et => et.Name == name).Any())
         {
             ModelAssociationSet mas = new ModelAssociationSet(this.ParentFile, this, name, fromEntitySet, toEntitySet, fromEntityType, toEntityType, fromMultiplicity, toMultiplicity, fromNavigationProperty, toNavigationProperty, keys);
             _modelAssociationSets.Add(mas.Name, mas);
             mas.NameChanged += new EventHandler<NameChangeArgs>(aset_NameChanged);
             mas.Removed += new EventHandler(aset_Removed);
             return mas;
         }
         else
         {
             throw new ArgumentException("An association named " + name + " already exists in the model.");
         }
     }
     catch (Exception ex)
     {
         try
         {
             if (!ex.Data.Contains("EDMXType"))
             {
                 ex.Data.Add("EDMXType", this.GetType().Name);
             }
             if (!ex.Data.Contains("EDMXObjectName"))
             {
                 ex.Data.Add("EDMXObjectName", this.ContainerName);
             }
         }
         catch { }
         throw;
     }
 }
 /// <summary>
 /// Adds a new entity type to the storage model, based on an existing conceptual model entity type.
 /// </summary>
 /// <param name="modelEntityType">Conceptual model entity type to use as a base.</param>
 /// <returns>A new StoreEntityType object.</returns>
 public StoreEntityType AddEntityType(ModelEntityType modelEntityType)
 {
     StoreEntityType storeEntityType = this.AddEntityType(modelEntityType.Name);
     if (!string.IsNullOrEmpty(modelEntityType.ShortDescription))
     {
         storeEntityType.ShortDescription = modelEntityType.ShortDescription;
     }
     if (!string.IsNullOrEmpty(modelEntityType.LongDescription))
     {
         storeEntityType.LongDescription = modelEntityType.LongDescription;
     }
     return storeEntityType;
 }
 /// <summary>
 /// Adds a store entityset to the model, based on an existing conceptual model entity set and entity type.
 /// </summary>
 /// <param name="modelEntitySet">Conceptual model entity set</param>
 /// <param name="modelEntityType">Conceptual model entity type</param>
 /// <returns>A new StoreEntitySet object.</returns>
 public StoreEntitySet AddEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType)
 {
     return(AddEntitySet(modelEntitySet, modelEntityType, "dbo", modelEntitySet.Name));
 }
 /// <summary>
 /// Creates and adds a new conceptual model entity type inheriting from an existing entity type.
 /// </summary>
 /// <param name="name">Entity type name for the new entity type.</param>
 /// <param name="baseType">Base type that this entity type inherits from.</param>
 /// <returns>A ModelEntityType instance corresponding to the new entity type.</returns>
 public ModelEntityType AddEntityType(string name, ModelEntityType baseType)
 {
     try
     {
         if (!EntityTypes.Any(et => et.Name == name)
             && !ComplexTypes.Any(ct => ct.Name == name))
         {
             ModelEntityType et = new ModelEntityType(ParentFile, this, name, baseType);
             _modelEntityTypes.Add(name, et);
             et.NameChanged += new EventHandler<NameChangeArgs>(et_NameChanged);
             et.Removed += new EventHandler(et_Removed);
             return et;
         }
         else
         {
             throw new ArgumentException("A type with the name " + name + " already exist in the model.");
         }
     }
     catch (Exception ex)
     {
         try
         {
             if (!ex.Data.Contains("EDMXType"))
             {
                 ex.Data.Add("EDMXType", this.GetType().Name);
             }
             if (!ex.Data.Contains("EDMXObjectName"))
             {
                 ex.Data.Add("EDMXObjectName", this.ContainerName);
             }
         }
         catch { }
         throw;
     }
 }
 /// <summary>
 /// True if the specified entity type inherits from this entity type.
 /// </summary>
 /// <param name="entityType">Entity type to look for in the subtypes of this entity.</param>
 /// <returns>True if found, false if not.</returns>
 public bool InheritsFrom(ModelEntityType entityType)
 {
     try
     {
         return SubTypes.Any(t => t == entityType);
     }
     catch (Exception ex)
     {
         try
         {
             ExceptionTools.AddExceptionData(ex, this);
         }
         catch { }
         throw;
     }
 }
        /// <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);
                    }
                }
            }
        }
 /// <summary>
 /// Retrieves an existing entityset based on model entityset/type, or creates a new one if a match can not be found.
 /// </summary>
 /// <param name="modelEntitySet">Conceptual model entity set to match.</param>
 /// <param name="modelEntityType">Conceptual model entity type to match.</param>
 /// <returns>A StoreEntitySet object.</returns>
 public StoreEntitySet GetOrCreateEntitySet(ModelEntitySet modelEntitySet, ModelEntityType modelEntityType)
 {
     return GetOrCreateEntitySet(modelEntitySet, modelEntityType, "dbo", modelEntitySet.Name);
 }
 /// <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>
 /// Retrieves an existing entitytype based on a conceptual model entittype, or creates a new one if a match can not be found.
 /// </summary>
 /// <param name="modelEntityType">Conceptual model entity type to match.</param>
 /// <returns>A StoreEntityType object.</returns>
 public StoreEntityType GetOrCreateEntityType(ModelEntityType modelEntityType)
 {
     StoreEntityType storeEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(modelEntityType.Name, StringComparison.InvariantCultureIgnoreCase));
     if (storeEntityType == null)
     {
         storeEntityType = AddEntityType(modelEntityType);
     }
     return storeEntityType;
 }
 internal NavigationProperty(EDMXFile parentFile, ModelEntityType modelEntityType, XmlElement navPropertyElement)
     : base(parentFile)
 {
     _modelEntityType = modelEntityType;
     _propertyElement = navPropertyElement;
 }
 internal NavigationProperty(EDMXFile parentFile, ModelEntityType modelEntityType, XmlElement navPropertyElement)
     : base(parentFile)
 {
     _modelEntityType = modelEntityType;
     _propertyElement = navPropertyElement;
 }
 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;
     }
 }