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 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; }
/// <summary> /// Adds a mapping between a model entity set and one or more store entitysets. /// </summary> /// <param name="modelEntitySet">Model entityset to add mapping for.</param> /// <param name="storeEntitySets">One or several store entitysets mapped to the model entity set.</param> /// <returns>An EntitySetMapping object.</returns> public EntitySetMapping AddMapping(ModelEntitySet modelEntitySet, params StoreEntitySet[] storeEntitySets) { EntitySetMapping esm = new EntitySetMapping(base.ParentFile, _entityContainerMapping, this, modelEntitySet, storeEntitySets); _entitySetMappings.Add(esm.ModelEntitySet.Name, esm); esm.Removed += new EventHandler(esm_Removed); esm.ModelEntitySet.NameChanged += new EventHandler<NameChangeArgs>(ModelEntitySet_NameChanged); return esm; }
void toEntitySet_Removed(object sender, EventArgs e) { _toEntitySet = null; }
void fromEntitySet_Removed(object sender, EventArgs e) { _fromEntitySet = null; }
/// <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; }
/// <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 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 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 set. /// </summary> /// <param name="name">Entity set name for the new entity</param> /// <returns>A ModelEntitySet instance corresponding to the new entity set.</returns> public ModelEntitySet AddEntitySet(string name) { try { if (!EntitySets.Where(es => es.Name == name).Any()) { ModelEntitySet es = new ModelEntitySet(ParentFile, this, name); _modelEntitySets.Add(name, es); es.NameChanged += new EventHandler<NameChangeArgs>(es_NameChanged); es.Removed += new EventHandler(es_Removed); return es; } else { throw new ArgumentException("An entity set 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> /// 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 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; } }