1 //--------------------------------------------------------------------- 2 // <copyright file="EntityFrameworkVersions.cs" company="Microsoft"> 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // </copyright> 5 // 6 // @owner Microsoft 7 // @backupOwner Microsoft 8 //--------------------------------------------------------------------- 9 using System; 10 using System.Collections.Generic; 11 using System.Text; 12 using System.Diagnostics; 13 using System.Reflection; 14 using System.Data.Metadata.Edm; 15 using System.Data.Entity.Design.Common; 16 using System.IO; 17 using System.Data.Mapping; 18 using System.Data.EntityModel.SchemaObjectModel; 19 using System.Linq; 20 using System.Xml; 21 22 namespace System.Data.Entity.Design 23 { 24 public static class EntityFrameworkVersions 25 { 26 public static readonly Version Version1 = EntityFrameworkVersionsUtil.Version1; 27 public static readonly Version Version2 = EntityFrameworkVersionsUtil.Version2; 28 public static readonly Version Version3 = EntityFrameworkVersionsUtil.Version3; 29 30 internal static Version EdmVersion1_1 { get { return EntityFrameworkVersionsUtil.EdmVersion1_1; } } 31 32 /// <summary> 33 /// Returns the stream of the XSD corresponding to the frameworkVersion, and dataSpace passed in. 34 /// </summary> 35 /// <param name="entityFrameworkVersion">The version of the EntityFramework that you want the Schema XSD for.</param> 36 /// <param name="dataSpace">The data space of the schem XSD that you want.</param> 37 /// <returns>Stream version of the XSD</returns> GetSchemaXsd(Version entityFrameworkVersion, DataSpace dataSpace)38 public static Stream GetSchemaXsd(Version entityFrameworkVersion, DataSpace dataSpace) 39 { 40 EDesignUtil.CheckTargetEntityFrameworkVersionArgument(entityFrameworkVersion, "entityFrameworkVersion"); 41 42 string resourceName = null; 43 switch(dataSpace) 44 { 45 case DataSpace.CSpace: 46 resourceName = GetEdmSchemaXsdResourceName(entityFrameworkVersion); 47 break; 48 case DataSpace.CSSpace: 49 resourceName = GetMappingSchemaXsdResourceName(entityFrameworkVersion); 50 break; 51 case DataSpace.SSpace: 52 resourceName = GetStoreSchemaXsdResourceName(entityFrameworkVersion); 53 break; 54 default: 55 throw EDesignUtil.Argument("dataSpace"); 56 } 57 58 Debug.Assert(!string.IsNullOrEmpty(resourceName), "Did you forget to map something new?"); 59 60 Assembly dataEntity = typeof(EdmItemCollection).Assembly; 61 return dataEntity.GetManifestResourceStream(resourceName); 62 } 63 GetStoreSchemaXsdResourceName(Version entityFrameworkVersion)64 private static string GetStoreSchemaXsdResourceName(Version entityFrameworkVersion) 65 { 66 Debug.Assert(IsValidVersion(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 67 Dictionary<string, XmlSchemaResource> map = new Dictionary<string, XmlSchemaResource>(); 68 XmlSchemaResource.AddStoreSchemaResourceMapEntries(map, GetEdmVersion(entityFrameworkVersion)); 69 return map[GetStoreSchemaNamespace(entityFrameworkVersion)].ResourceName; 70 71 } 72 GetMappingSchemaXsdResourceName(Version entityFrameworkVersion)73 private static string GetMappingSchemaXsdResourceName(Version entityFrameworkVersion) 74 { 75 Debug.Assert(IsValidVersion(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 76 Dictionary<string, XmlSchemaResource> map = new Dictionary<string, XmlSchemaResource>(); 77 XmlSchemaResource.AddMappingSchemaResourceMapEntries(map, GetEdmVersion(entityFrameworkVersion)); 78 return map[GetMappingSchemaNamespace(entityFrameworkVersion)].ResourceName; 79 } 80 GetEdmVersion(Version entityFrameworkVersion)81 private static double GetEdmVersion(Version entityFrameworkVersion) 82 { 83 Debug.Assert(IsValidVersion(entityFrameworkVersion), "Did you add a new version or forget to check the version"); 84 if (entityFrameworkVersion.Major == 1) 85 { 86 if (entityFrameworkVersion.Minor == 1) 87 { 88 return XmlConstants.EdmVersionForV1_1; 89 } 90 else 91 { 92 return XmlConstants.EdmVersionForV1; 93 } 94 } 95 else if (entityFrameworkVersion.Major == 2) 96 { 97 return XmlConstants.EdmVersionForV2; 98 } 99 else 100 { 101 Debug.Assert(entityFrameworkVersion == EntityFrameworkVersions.Version3, "did you add a new version?"); 102 return XmlConstants.EdmVersionForV3; 103 } 104 } 105 GetEdmSchemaXsdResourceName(Version entityFrameworkVersion)106 private static string GetEdmSchemaXsdResourceName(Version entityFrameworkVersion) 107 { 108 Debug.Assert(ValidVersions.Contains(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 109 Dictionary<string, XmlSchemaResource> map = new Dictionary<string, XmlSchemaResource>(); 110 XmlSchemaResource.AddEdmSchemaResourceMapEntries(map, GetEdmVersion(entityFrameworkVersion)); 111 return map[GetEdmSchemaNamespace(entityFrameworkVersion)].ResourceName; 112 } 113 GetSchemaNamespace(Version entityFrameworkVersion, DataSpace dataSpace)114 internal static string GetSchemaNamespace(Version entityFrameworkVersion, DataSpace dataSpace) 115 { 116 Debug.Assert(IsValidVersion(entityFrameworkVersion), "Did you add a new version or forget to check the version"); 117 Debug.Assert(dataSpace == DataSpace.CSpace || 118 dataSpace == DataSpace.CSSpace || 119 dataSpace == DataSpace.SSpace, "only support the three spaces with an xml file format"); 120 switch (dataSpace) 121 { 122 case DataSpace.CSpace: 123 return GetEdmSchemaNamespace(entityFrameworkVersion); 124 case DataSpace.SSpace: 125 return GetStoreSchemaNamespace(entityFrameworkVersion); 126 default: 127 return GetMappingSchemaNamespace(entityFrameworkVersion); 128 } 129 } 130 GetStoreSchemaNamespace(Version entityFrameworkVersion)131 private static string GetStoreSchemaNamespace(Version entityFrameworkVersion) 132 { 133 Debug.Assert(ValidVersions.Contains(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 134 if (entityFrameworkVersion == EntityFrameworkVersions.Version1) 135 { 136 return XmlConstants.TargetNamespace_1; 137 } 138 else if (entityFrameworkVersion == EntityFrameworkVersions.Version2) 139 { 140 return XmlConstants.TargetNamespace_2; 141 } 142 else 143 { 144 Debug.Assert(entityFrameworkVersion == EntityFrameworkVersions.Version3, "did you add a new version?"); 145 return XmlConstants.TargetNamespace_3; 146 } 147 } 148 GetMappingSchemaNamespace(Version entityFrameworkVersion)149 private static string GetMappingSchemaNamespace(Version entityFrameworkVersion) 150 { 151 Debug.Assert(ValidVersions.Contains(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 152 if (entityFrameworkVersion == EntityFrameworkVersions.Version1) 153 { 154 return StorageMslConstructs.NamespaceUriV1; 155 } 156 else if (entityFrameworkVersion == EntityFrameworkVersions.Version2) 157 { 158 return StorageMslConstructs.NamespaceUriV2; 159 } 160 else 161 { 162 Debug.Assert(entityFrameworkVersion == EntityFrameworkVersions.Version3, "did you add a new version?"); 163 return StorageMslConstructs.NamespaceUriV3; 164 } 165 } 166 GetEdmSchemaNamespace(Version entityFrameworkVersion)167 private static string GetEdmSchemaNamespace(Version entityFrameworkVersion) 168 { 169 Debug.Assert(ValidVersions.Contains(entityFrameworkVersion), "Did you forget to check for valid versions before calling this private method?"); 170 if (entityFrameworkVersion == EntityFrameworkVersions.Version1) 171 { 172 return XmlConstants.ModelNamespace_1; 173 } 174 else if (entityFrameworkVersion == EntityFrameworkVersions.Version2) 175 { 176 return XmlConstants.ModelNamespace_2; 177 } 178 else 179 { 180 Debug.Assert(entityFrameworkVersion == EntityFrameworkVersions.Version3, "did you add a new version?"); 181 return XmlConstants.ModelNamespace_3; 182 } 183 } 184 185 internal static Version Default = Version2; 186 internal static Version Latest = Version3; 187 internal static Version[] ValidVersions = new Version[] { Version1, Version2, Version3 }; IsValidVersion(Version entityFrameworkVersion)188 internal static bool IsValidVersion(Version entityFrameworkVersion) 189 { 190 return ValidVersions.Contains(entityFrameworkVersion); 191 } 192 193 // this method will skip down to the first element, or to the end if it doesn't find one TryGetEdmxVersion(XmlReader reader, out Version entityFrameworkVersion)194 internal static bool TryGetEdmxVersion(XmlReader reader, out Version entityFrameworkVersion) 195 { 196 // to make life simpler, we skip down to the first/root element, unless we're 197 // already there 198 if (!reader.EOF && reader.NodeType != XmlNodeType.Element) 199 { 200 while (reader.Read() && reader.NodeType != XmlNodeType.Element) 201 { 202 } 203 } 204 205 if (!reader.EOF && 206 (reader.LocalName == EntityDesignerUtils.EdmxRootElementName)) 207 { 208 return TryGetEdmxVersion(reader.NamespaceURI, out entityFrameworkVersion); 209 } 210 211 entityFrameworkVersion = default(Version); 212 return false; 213 } 214 TryGetEdmxVersion(string xmlNamespaceName, out Version entityFrameworkVersion)215 internal static bool TryGetEdmxVersion(string xmlNamespaceName, out Version entityFrameworkVersion) 216 { 217 switch (xmlNamespaceName) 218 { 219 case EntityDesignerUtils.EdmxNamespaceUriV1: 220 entityFrameworkVersion = EntityFrameworkVersions.Version1; 221 return true; 222 case EntityDesignerUtils.EdmxNamespaceUriV2: 223 entityFrameworkVersion = EntityFrameworkVersions.Version2; 224 return true; 225 case EntityDesignerUtils.EdmxNamespaceUriV3: 226 entityFrameworkVersion = EntityFrameworkVersions.Version3; 227 return true; 228 default: 229 entityFrameworkVersion = default(Version); 230 return false; 231 } 232 } 233 234 } 235 } 236