PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PowerTools/Utilities/EdmxUtility.cs

#
C# | 148 lines | 129 code | 19 blank | 0 comment | 7 complexity | f5364a66220ea97f49e80000b558d0c4 MD5 | raw file
  1. namespace Microsoft.DbContextPackage.Utilities
  2. {
  3. using System.Collections.Generic;
  4. using System.Data.Entity.Design;
  5. using System.Data.Mapping;
  6. using System.Data.Metadata.Edm;
  7. using System.Diagnostics.Contracts;
  8. using System.IO;
  9. using System.Xml;
  10. using System.Xml.Linq;
  11. using Microsoft.DbContextPackage.Extensions;
  12. using Microsoft.DbContextPackage.Resources;
  13. internal class EdmxUtility
  14. {
  15. private static readonly IEnumerable<XNamespace> EDMX_NAMESPACES = new XNamespace[]
  16. {
  17. "http://schemas.microsoft.com/ado/2009/11/edmx",
  18. "http://schemas.microsoft.com/ado/2008/10/edmx",
  19. "http://schemas.microsoft.com/ado/2007/06/edmx"
  20. };
  21. private readonly string _edmxPath;
  22. public EdmxUtility(string edmxPath)
  23. {
  24. Contract.Requires(!string.IsNullOrWhiteSpace(edmxPath));
  25. _edmxPath = edmxPath;
  26. }
  27. public StorageMappingItemCollection GetMappingCollection()
  28. {
  29. IList<EdmSchemaError> errors;
  30. var edmxFileName = Path.GetFileName(_edmxPath);
  31. EdmItemCollection edmCollection;
  32. using (var reader = CreateSectionReader(EdmxSection.Csdl))
  33. {
  34. edmCollection = MetadataItemCollectionFactory.CreateEdmItemCollection(
  35. new[] { reader },
  36. out errors);
  37. errors.HandleErrors(Strings.EdmSchemaError(edmxFileName, EdmxSection.Csdl.SectionName));
  38. }
  39. StoreItemCollection storeCollection;
  40. using (var reader = CreateSectionReader(EdmxSection.Ssdl))
  41. {
  42. storeCollection = MetadataItemCollectionFactory.CreateStoreItemCollection(
  43. new[] { reader },
  44. out errors);
  45. errors.HandleErrors(Strings.EdmSchemaError(edmxFileName, EdmxSection.Ssdl.SectionName));
  46. }
  47. StorageMappingItemCollection mappingCollection;
  48. using (var reader = CreateSectionReader(EdmxSection.Msl))
  49. {
  50. mappingCollection = MetadataItemCollectionFactory.CreateStorageMappingItemCollection(
  51. edmCollection,
  52. storeCollection,
  53. new[] { reader },
  54. out errors);
  55. errors.HandleErrors(Strings.EdmSchemaError(edmxFileName, EdmxSection.Msl.SectionName));
  56. }
  57. return mappingCollection;
  58. }
  59. private XmlReader CreateSectionReader(EdmxSection edmxSection)
  60. {
  61. Contract.Requires(edmxSection != null);
  62. var edmxDocument = XElement.Load(_edmxPath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
  63. var runtime = edmxDocument.Element(EDMX_NAMESPACES, "Runtime");
  64. if (runtime == null)
  65. {
  66. return null;
  67. }
  68. var section = runtime.Element(EDMX_NAMESPACES, edmxSection.SectionName);
  69. if (section == null)
  70. {
  71. return null;
  72. }
  73. var rootElement = section.Element(edmxSection.Namespaces, edmxSection.RootElementName);
  74. if (rootElement == null)
  75. {
  76. return null;
  77. }
  78. return rootElement.CreateReader();
  79. }
  80. private sealed class EdmxSection
  81. {
  82. static EdmxSection()
  83. {
  84. Csdl = new EdmxSection
  85. {
  86. Namespaces = new XNamespace[]
  87. {
  88. "http://schemas.microsoft.com/ado/2009/11/edm",
  89. "http://schemas.microsoft.com/ado/2008/09/edm",
  90. "http://schemas.microsoft.com/ado/2006/04/edm"
  91. },
  92. SectionName = "ConceptualModels",
  93. RootElementName = "Schema"
  94. };
  95. Msl = new EdmxSection
  96. {
  97. Namespaces = new XNamespace[]
  98. {
  99. "http://schemas.microsoft.com/ado/2009/11/mapping/cs",
  100. "http://schemas.microsoft.com/ado/2008/09/mapping/cs",
  101. "urn:schemas-microsoft-com:windows:storage:mapping:CS"
  102. },
  103. SectionName = "Mappings",
  104. RootElementName = "Mapping"
  105. };
  106. Ssdl = new EdmxSection
  107. {
  108. Namespaces = new XNamespace[]
  109. {
  110. "http://schemas.microsoft.com/ado/2009/11/edm/ssdl",
  111. "http://schemas.microsoft.com/ado/2009/02/edm/ssdl",
  112. "http://schemas.microsoft.com/ado/2006/04/edm/ssdl"
  113. },
  114. SectionName = "StorageModels",
  115. RootElementName = "Schema"
  116. };
  117. }
  118. private EdmxSection()
  119. {
  120. }
  121. public static EdmxSection Csdl { get; private set; }
  122. public static EdmxSection Msl { get; private set; }
  123. public static EdmxSection Ssdl { get; private set; }
  124. public IEnumerable<XNamespace> Namespaces { get; private set; }
  125. public string SectionName { get; private set; }
  126. public string RootElementName { get; private set; }
  127. }
  128. }
  129. }