/// <summary> /// Reads an EDMX file from a stream. /// </summary> /// <param name="edmxStream">Stream to read the EDMX data from.</param> public EDMXFile(System.IO.Stream edmxStream) { //load edmx stream _edmxDocument = new XmlDocument(); _edmxDocument.Load(edmxStream); //get namespace manager _nsm = EDMXUtils.GetNamespaceManager(_edmxDocument, out _edmxVersion); //ensure the file is in the expected format... switch (_edmxVersion) { case EDMXVersionEnum.EDMX2012: case EDMXVersionEnum.EDMX2010: //expected format break; case EDMXVersionEnum.EDMX2008: throw new NotSupportedException("The EDMX file is in VS2008 / EFv1 format. This tool only support VS2010/EFv4 EDMX files"); //break; default: throw new NotSupportedException("Unknown EDMX file format."); } }
/// <summary> /// Creates a new EDMX file with the given model name, provider and provider manifest token. /// </summary> /// <param name="modelName">Name of the new model, e.g. "AdventureWorks".</param> /// <param name="dbProvider">Database provider name, e.g. "System.Data.SqlClient"</param> /// <param name="providerManifestToken">Provider manifest token, e.g. "2008"</param> /// <param name="version">EDMX file version</param> public EDMXFile(string modelName, string dbProvider, string providerManifestToken, EDMXVersionEnum version) { //new blank EDMX File _edmxDocument = new XmlDocument(); string schemaURI = string.Empty; string versionNumber = string.Empty; switch (version) { case EDMXVersionEnum.EDMX2008: schemaURI = "http://schemas.microsoft.com/ado/2007/06/edmx"; versionNumber = "1.0"; break; case EDMXVersionEnum.EDMX2010: schemaURI = "http://schemas.microsoft.com/ado/2008/10/edmx"; versionNumber = "2.0"; break; case EDMXVersionEnum.EDMX2012: schemaURI = "http://schemas.microsoft.com/ado/2009/11/edmx"; versionNumber = "3.0"; break; } //create document element XmlElement docElement = _edmxDocument.CreateElement("Edmx", schemaURI); docElement.SetAttribute("Version", versionNumber); _edmxDocument.AppendChild(docElement); //get hold of the namespace manager _nsm = EDMXUtils.GetNamespaceManager(_edmxDocument, out _edmxVersion); CreateEmptyEDMX(); //set required storage model properties this.StorageModel.Namespace = modelName + ".Store"; this.StorageModel.Provider = dbProvider; this.StorageModel.ProviderManifestToken = providerManifestToken; this.StorageModel.ContainerName = modelName + "StoreContainer"; //set required conceptial model properties this.ConceptualModel.Namespace = modelName; this.ConceptualModel.ContainerName = modelName + "Entities"; //set required mapping properties this.CSMapping.StorageEntityContainer = this.StorageModel.ContainerName; this.CSMapping.ConceptualEntityContainer = this.ConceptualModel.ContainerName; //set required designer properties this.Designer.DiagramName = modelName; }
/// <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; }