/// <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); } }
internal MappingCondition(EDMXFile parentFile, HuagatiEDMXTools.EntitySetMapping entitySetMapping, XmlElement conditionElement) : base(parentFile) { _entitySetMapping = entitySetMapping; _mappingCondition = conditionElement; _mappingFragment = (XmlElement)_mappingCondition.ParentNode; _entityTypeMapping = (XmlElement)_mappingFragment.ParentNode; string entityTypeName = EDMXUtils.StripTypeOf(_entityTypeMapping.GetAttribute("TypeName")); _modelEntityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase) || et.AliasName.Equals(entityTypeName, StringComparison.InvariantCultureIgnoreCase)); if (_modelEntityType != null) { string columnName = _mappingCondition.GetAttribute("ColumnName"); _discriminatorColumn = EntitySetMapping.StoreEntitySetsFor(_modelEntityType).SelectMany(c => c.EntityType.MemberProperties).FirstOrDefault(mp => mp.Name.Equals(columnName, StringComparison.InvariantCultureIgnoreCase)); } }
/// <summary> /// Adds a complex type mapping /// </summary> /// <param name="complexTypeReference">Model member property referencing the complex type property</param> /// <param name="memberProperty">Model member property</param> /// <param name="storeMemberProperty">Store member property</param> public void AddComplexMapping(ModelMemberProperty complexTypeReference, ModelMemberProperty memberProperty, StoreMemberProperty storeMemberProperty) { //find the appropriate mapping fragment string storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name; foreach (XmlElement mappingFragment in _esmElement.SelectNodes("map:EntityTypeMapping/map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM)) { if (mappingFragment != null) { XmlElement complexProperty = (XmlElement)mappingFragment.SelectSingleNode("map:ComplexProperty[@Name=" + XmlHelpers.XPathLiteral(complexTypeReference.Name) + "]", NSM); if (complexProperty == null) { complexProperty = EDMXDocument.CreateElement("ComplexProperty", NameSpaceURImap); complexProperty.SetAttribute("Name", complexTypeReference.Name); complexProperty.SetAttribute("TypeName", complexTypeReference.TypeName); mappingFragment.AppendChild(complexProperty); } string entityTypeName = EDMXUtils.StripTypeOf(((XmlElement)mappingFragment.ParentNode).GetAttribute("TypeName")); ModelEntityType entityType = ParentFile.ConceptualModel.EntityTypes.FirstOrDefault(et => et.FullName == entityTypeName || et.AliasName == entityTypeName); XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap); scalarProperty.SetAttribute("Name", memberProperty.Name); scalarProperty.SetAttribute("ColumnName", storeMemberProperty.Name); complexProperty.AppendChild(scalarProperty); _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(storeMemberProperty, memberProperty, entityType)); storeMemberProperty.Removed += new EventHandler(smp_Removed); memberProperty.Removed += new EventHandler(mmp_Removed); } else { throw new ArgumentException("The store entity set " + storeEntitySetName + " is not associated with the model entity set " + this.ModelEntitySet.Name); } } }
private void 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; }