TableKey.tt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF.Utility.CS.ttinclude"#>
  3. <#@ assembly name="$(DevEnvDir)Microsoft.Data.Entity.Design.DatabaseGeneration.dll"#>
  4. <#@ import namespace="Microsoft.Data.Entity.Design.DatabaseGeneration" #>
  5. <#@ import namespace="System.Text"#>
  6. <#@ output extension=".cs"#><#
  7. CodeGenerationTools code = new CodeGenerationTools(this);
  8. CodeRegion region = new CodeRegion(this, 1);
  9. MetadataTools ef = new MetadataTools(this);
  10. string inputFile = @"EMISContext.edmx";
  11. var loadResult = LoadMetadata(inputFile);
  12. EdmItemCollection itemCollection = loadResult.EdmItems;
  13. var propertyToColumnMapping = loadResult.PropertyToColumnMapping;
  14. var manyToManyMappings = loadResult.ManyToManyMappings;
  15. var tphMappings = loadResult.TphMappings;
  16. string namespaceName = code.VsNamespaceSuggestion();
  17. EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
  18. WriteHeader(fileManager);#>
  19. using System.Collections.Generic;
  20. namespace EMIS.Entities
  21. {
  22. public class TableKeyDictionary
  23. {
  24. private static Dictionary<string, string> items;
  25. public static Dictionary<string, string> Items
  26. {
  27. get { return items; }
  28. }
  29. static TableKeyDictionary()
  30. {
  31. items = new Dictionary<string, string>();
  32. <#foreach (EntityType entity in itemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  33. {
  34. string hasKey;
  35. hasKey = string.Format("items.Add(\"{0}\", \"{1}\");", entity.Name, entity.KeyMembers[0].Name);#>
  36. <#=hasKey#>
  37. <#}
  38. #>
  39. }
  40. public static string GetKeyName<TEntity>(TEntity entity)
  41. {
  42. string tableName = entity.GetType().Name;
  43. return items[tableName];
  44. }
  45. public static string GetKeyName<TEntity>()
  46. {
  47. string tableName = typeof(TEntity).Name;
  48. return items[tableName];
  49. }
  50. }
  51. }
  52. <#+
  53. string GetResourceString(string resourceName)
  54. {
  55. if(_resourceManager2 == null)
  56. {
  57. _resourceManager2 = new System.Resources.ResourceManager("System.Data.Entity.Design", typeof(System.Data.Entity.Design.MetadataItemCollectionFactory).Assembly);
  58. }
  59. return _resourceManager2.GetString(resourceName, null);
  60. }
  61. System.Resources.ResourceManager _resourceManager2;
  62. void WriteHeader(EntityFrameworkTemplateFileManager fileManager)
  63. {
  64. fileManager.StartHeader();
  65. #>
  66. //------------------------------------------------------------------------------
  67. // <auto-generated>
  68. // This code was generated from a template.
  69. //
  70. // Manual changes to this file may cause unexpected behavior in your application.
  71. // Manual changes to this file will be overwritten if the code is regenerated.
  72. // </auto-generated>
  73. //------------------------------------------------------------------------------
  74. <#+
  75. fileManager.EndBlock();
  76. }
  77. void BeginNamespace(string namespaceName, CodeGenerationTools code)
  78. {
  79. CodeRegion region = new CodeRegion(this);
  80. if (!String.IsNullOrEmpty(namespaceName))
  81. {
  82. #>
  83. namespace <#=code.EscapeNamespace(namespaceName)#>
  84. {
  85. <#+
  86. PushIndent(CodeRegion.GetIndent(1));
  87. }
  88. }
  89. void EndNamespace(string namespaceName)
  90. {
  91. if (!String.IsNullOrEmpty(namespaceName))
  92. {
  93. PopIndent();
  94. #>
  95. }
  96. <#+
  97. }
  98. }
  99. string ToTable(EntitySet entitySet)
  100. {
  101. var toTable = entitySet.Name;
  102. string schema = entitySet.GetSchemaName();
  103. if(!string.IsNullOrWhiteSpace(schema) && schema != "dbo")
  104. toTable += "\", \"" + schema;
  105. return toTable;
  106. }
  107. bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
  108. {
  109. var alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  110. foreach(var type in itemCollection.GetItems<StructuralType>())
  111. {
  112. if (!(type is EntityType || type is ComplexType))
  113. {
  114. continue;
  115. }
  116. if (alreadySeen.ContainsKey(type.FullName))
  117. {
  118. Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
  119. return false;
  120. }
  121. else
  122. {
  123. alreadySeen.Add(type.FullName, true);
  124. }
  125. }
  126. return true;
  127. }
  128. string GetGenerationOption(EdmProperty property, EntityType entity)
  129. {
  130. string result = "";
  131. bool isPk = entity.KeyMembers.Contains(property);
  132. MetadataProperty storeGeneratedPatternProperty = null;
  133. string storeGeneratedPatternPropertyValue = "None";
  134. if(property.MetadataProperties.TryGetValue(MetadataConsts.EDM_ANNOTATION_09_02 + ":StoreGeneratedPattern", false, out storeGeneratedPatternProperty))
  135. storeGeneratedPatternPropertyValue = storeGeneratedPatternProperty.Value.ToString();
  136. PrimitiveType edmType = (PrimitiveType) property.TypeUsage.EdmType;
  137. if (storeGeneratedPatternPropertyValue == "Computed")
  138. {
  139. result = ".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)";
  140. }
  141. else if ((edmType.ClrEquivalentType == typeof(int)) || (edmType.ClrEquivalentType == typeof(short)) || (edmType.ClrEquivalentType == typeof(long)))
  142. {
  143. if (isPk && (storeGeneratedPatternPropertyValue != "Identity"))
  144. result = ".HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)";
  145. else if ((!isPk || (entity.KeyMembers.Count > 1)) && (storeGeneratedPatternPropertyValue == "Identity"))
  146. result = ".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)";
  147. }
  148. return result;
  149. }
  150. MetadataLoadResult LoadMetadata(string inputFile)
  151. {
  152. var loader = new MetadataLoader(this);
  153. bool loaded = false;
  154. EdmItemCollection edmItemCollection = loader.CreateEdmItemCollection(inputFile);
  155. StoreItemCollection storeItemCollection = null;
  156. if (loader.TryCreateStoreItemCollection(inputFile, out storeItemCollection))
  157. {
  158. StorageMappingItemCollection storageMappingItemCollection;
  159. if (loader.TryCreateStorageMappingItemCollection(inputFile, edmItemCollection, storeItemCollection, out storageMappingItemCollection))
  160. loaded = true;
  161. }
  162. if(loaded == false)
  163. throw new Exception("Cannot load a metadata from the file " + inputFile);
  164. var mappingMetadata = LoadMappingMetadata(inputFile);
  165. var mappingNode = mappingMetadata.Item1;
  166. var nsmgr = mappingMetadata.Item2;
  167. var allEntitySets = storeItemCollection.GetAllEntitySets();
  168. return new MetadataLoadResult
  169. {
  170. EdmItems = edmItemCollection,
  171. PropertyToColumnMapping = BuildEntityMappings(mappingNode, nsmgr, edmItemCollection.GetItems<EntityType>(), edmItemCollection.GetAllEntitySets(), allEntitySets),
  172. ManyToManyMappings = BuildManyToManyMappings(mappingNode, nsmgr, edmItemCollection.GetAllAssociationSets(), allEntitySets),
  173. TphMappings=BuildTPHMappings(mappingNode, nsmgr, edmItemCollection.GetItems<EntityType>(), edmItemCollection.GetAllEntitySets(), allEntitySets)
  174. };
  175. }
  176. private Tuple<XmlNode, XmlNamespaceManager> LoadMappingMetadata(string inputFile)
  177. {
  178. var xmlDoc = new XmlDocument();
  179. xmlDoc.Load(Host.ResolvePath(inputFile));
  180. var schemaConstantsList = new SchemaConsts[]
  181. {
  182. MetadataConsts.V2_SCHEMA_CONSTANTS,
  183. MetadataConsts.V1_SCHEMA_CONSTANTS
  184. };
  185. foreach (var schemaConstants in schemaConstantsList)
  186. {
  187. var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
  188. nsmgr.AddNamespace("ef", schemaConstants.MslNamespace);
  189. nsmgr.AddNamespace("edmx", schemaConstants.EdmxNamespace);
  190. var mappingNode = xmlDoc.DocumentElement.SelectSingleNode("./*/edmx:Mappings", nsmgr);
  191. if(mappingNode != null)
  192. return Tuple.Create(mappingNode, nsmgr);
  193. }
  194. throw new Exception(GetResourceString("Template_UnsupportedSchema"));
  195. }
  196. private Dictionary<EntityType, Dictionary<EntityType, Dictionary<EdmProperty, string>>> BuildTPHMappings(XmlNode mappingNode, XmlNamespaceManager nsmgr, IEnumerable<EntityType> entityTypes, IEnumerable<EntitySet> entitySets, IEnumerable<EntitySet> tableSets)
  197. {
  198. var dictionary = new Dictionary<EntityType, Dictionary<EntityType, Dictionary<EdmProperty, string>>>();
  199. foreach (EntitySet set in entitySets)
  200. {
  201. XmlNodeList nodes = mappingNode.SelectNodes(string.Format(".//ef:EntitySetMapping[@Name=\"{0}\"]/ef:EntityTypeMapping/ef:MappingFragment", set.Name), nsmgr);
  202. foreach(XmlNode node in nodes)
  203. {
  204. string typeName=node.ParentNode.Attributes["TypeName"].Value;
  205. if(typeName.StartsWith("IsTypeOf("))
  206. typeName=typeName.Substring("IsTypeOf(".Length, typeName.Length-"IsTypeOf()".Length);
  207. EntityType type=entityTypes.Single(z=>z.FullName==typeName);
  208. string tableName = node.Attributes["StoreEntitySet"].Value;
  209. EntitySet set2 = tableSets.Single(entitySet => entitySet.Name == tableName);
  210. var entityMap = new Dictionary<EdmProperty, string>();
  211. XmlNodeList propertyNodes = node.SelectNodes("./ef:Condition", nsmgr);
  212. if(propertyNodes.Count==0) continue;
  213. foreach(XmlNode propertyNode in propertyNodes)
  214. {
  215. string str = propertyNode.Attributes["ColumnName"].Value;
  216. EdmProperty property2 = set2.ElementType.Properties[str];
  217. string val=propertyNode.Attributes["Value"].Value;
  218. entityMap.Add(property2, val);
  219. }
  220. EntityType baseType=(EntityType)(type.BaseType??type);
  221. if(!dictionary.Keys.Contains(baseType))
  222. {
  223. var entityMappings=new Dictionary<EntityType, Dictionary<EdmProperty, string>>();
  224. //entityMappings.Add(type,entityMap);
  225. dictionary.Add(baseType, entityMappings);
  226. }
  227. dictionary[baseType].Add(type,entityMap);
  228. }
  229. }
  230. return dictionary;
  231. }
  232. private Dictionary<EntityType, Tuple<EntitySet, Dictionary<EdmProperty, EdmProperty>>> BuildEntityMappings(XmlNode mappingNode, XmlNamespaceManager nsmgr, IEnumerable<EntityType> entityTypes, IEnumerable<EntitySet> entitySets, IEnumerable<EntitySet> tableSets)
  233. {
  234. var dictionary = new Dictionary<EntityType, Tuple<EntitySet, Dictionary<EdmProperty, EdmProperty>>>();
  235. foreach (EntitySet set in entitySets)
  236. {
  237. XmlNodeList nodes = mappingNode.SelectNodes(string.Format(".//ef:EntitySetMapping[@Name=\"{0}\"]/ef:EntityTypeMapping/ef:MappingFragment", set.Name), nsmgr);
  238. foreach(XmlNode node in nodes)
  239. {
  240. string typeName=node.ParentNode.Attributes["TypeName"].Value;
  241. if(typeName.StartsWith("IsTypeOf("))
  242. typeName=typeName.Substring("IsTypeOf(".Length, typeName.Length-"IsTypeOf()".Length);
  243. EntityType type=entityTypes.Single(z=>z.FullName==typeName);
  244. string tableName = node.Attributes["StoreEntitySet"].Value;
  245. EntitySet set2 = tableSets.Single(entitySet => entitySet.Name == tableName);
  246. var entityMap = new Dictionary<EdmProperty, EdmProperty>();
  247. foreach (EdmProperty property in type.Properties)
  248. {
  249. XmlNode propertyNode = node.SelectSingleNode(string.Format("./ef:ScalarProperty[@Name=\"{0}\"]", property.Name), nsmgr);
  250. if(propertyNode == null) continue;
  251. string str = propertyNode.Attributes["ColumnName"].Value;
  252. EdmProperty property2 = set2.ElementType.Properties[str];
  253. entityMap.Add(property, property2);
  254. }
  255. dictionary.Add(type, Tuple.Create(set2, entityMap));
  256. }
  257. }
  258. return dictionary;
  259. }
  260. Dictionary<AssociationType, Tuple<EntitySet, Dictionary<RelationshipEndMember, Dictionary<EdmMember, string>>>> BuildManyToManyMappings(XmlNode mappingNode, XmlNamespaceManager nsmgr, IEnumerable<AssociationSet> associationSets, IEnumerable<EntitySet> tableSets)
  261. {
  262. var dictionary = new Dictionary<AssociationType, Tuple<EntitySet, Dictionary<RelationshipEndMember, Dictionary<EdmMember, string>>>>();
  263. foreach (AssociationSet associationSet in associationSets.Where(set => set.ElementType.IsManyToMany()))
  264. {
  265. XmlNode node = mappingNode.SelectSingleNode(string.Format("//ef:AssociationSetMapping[@Name=\"{0}\"]", associationSet.Name), nsmgr);
  266. string tableName = node.Attributes["StoreEntitySet"].Value;
  267. EntitySet entitySet = tableSets.Single(s => s.Name == tableName);
  268. var relationEndMap = new Dictionary<RelationshipEndMember, Dictionary<EdmMember, string>>();
  269. foreach (AssociationSetEnd end in associationSet.AssociationSetEnds)
  270. {
  271. var map = new Dictionary<EdmMember, string>();
  272. foreach (XmlNode endProperty in node.SelectSingleNode(string.Format("./ef:EndProperty[@Name=\"{0}\"]", end.Name), nsmgr).ChildNodes)
  273. {
  274. string str = endProperty.Attributes["Name"].Value;
  275. EdmProperty key = end.EntitySet.ElementType.Properties[str];
  276. string str2 = endProperty.Attributes["ColumnName"].Value;
  277. map.Add(key, str2);
  278. }
  279. relationEndMap.Add(end.CorrespondingAssociationEndMember, map);
  280. }
  281. dictionary.Add(associationSet.ElementType, Tuple.Create(entitySet, relationEndMap));
  282. }
  283. return dictionary;
  284. }
  285. public class MetadataLoadResult
  286. {
  287. public EdmItemCollection EdmItems { get; set; }
  288. public Dictionary<EntityType, Tuple<EntitySet, Dictionary<EdmProperty, EdmProperty>>> PropertyToColumnMapping { get; set; }
  289. public Dictionary<AssociationType, Tuple<EntitySet, Dictionary<RelationshipEndMember, Dictionary<EdmMember, string>>>> ManyToManyMappings { get; set; }
  290. public Dictionary<EntityType, Dictionary<EntityType, Dictionary<EdmProperty, string>>> TphMappings { get; set; }
  291. }
  292. /// <summary>
  293. /// Responsible for encapsulating the constants defined in Metadata
  294. /// </summary>
  295. public static class MetadataConsts
  296. {
  297. public const string CSDL_EXTENSION = ".csdl";
  298. public const string CSDL_EDMX_SECTION_NAME = "ConceptualModels";
  299. public const string CSDL_ROOT_ELEMENT_NAME = "Schema";
  300. public const string EDM_ANNOTATION_09_02 = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";
  301. public const string SSDL_EXTENSION = ".ssdl";
  302. public const string SSDL_EDMX_SECTION_NAME = "StorageModels";
  303. public const string SSDL_ROOT_ELEMENT_NAME = "Schema";
  304. public const string MSL_EXTENSION = ".msl";
  305. public const string MSL_EDMX_SECTION_NAME = "Mappings";
  306. public const string MSL_ROOT_ELEMENT_NAME = "Mapping";
  307. public const string TT_TEMPLATE_NAME = "TemplateName";
  308. public const string TT_TEMPLATE_VERSION = "TemplateVersion";
  309. public const string TT_MINIMUM_ENTITY_FRAMEWORK_VERSION = "MinimumEntityFrameworkVersion";
  310. public const string DEFAULT_TEMPLATE_VERSION = "4.0";
  311. public static readonly SchemaConsts V1_SCHEMA_CONSTANTS = new SchemaConsts(
  312. "http://schemas.microsoft.com/ado/2007/06/edmx",
  313. "http://schemas.microsoft.com/ado/2006/04/edm",
  314. "http://schemas.microsoft.com/ado/2006/04/edm/ssdl",
  315. "urn:schemas-microsoft-com:windows:storage:mapping:CS",
  316. new Version("3.5"));
  317. public static readonly SchemaConsts V2_SCHEMA_CONSTANTS = new SchemaConsts(
  318. "http://schemas.microsoft.com/ado/2008/10/edmx",
  319. "http://schemas.microsoft.com/ado/2008/09/edm",
  320. "http://schemas.microsoft.com/ado/2009/02/edm/ssdl",
  321. "http://schemas.microsoft.com/ado/2008/09/mapping/cs",
  322. new Version("4.0"));
  323. public static readonly SchemaConsts V3_SCHEMA_CONSTANTS = new SchemaConsts(
  324. "http://schemas.microsoft.com/ado/2009/11/edmx",
  325. "http://schemas.microsoft.com/ado/2009/11/edm",
  326. "http://schemas.microsoft.com/ado/2009/11/edm/ssdl",
  327. "http://schemas.microsoft.com/ado/2009/11/mapping/cs",
  328. new Version("4.5"));
  329. }
  330. public struct SchemaConsts
  331. {
  332. public SchemaConsts(string edmxNamespace, string csdlNamespace, string ssdlNamespace, string mslNamespace, Version minimumTemplateVersion) : this()
  333. {
  334. EdmxNamespace = edmxNamespace;
  335. CsdlNamespace = csdlNamespace;
  336. SsdlNamespace = ssdlNamespace;
  337. MslNamespace = mslNamespace;
  338. MinimumTemplateVersion = minimumTemplateVersion;
  339. }
  340. public string EdmxNamespace { get; private set; }
  341. public string CsdlNamespace { get; private set; }
  342. public string SsdlNamespace { get; private set; }
  343. public string MslNamespace { get; private set; }
  344. public Version MinimumTemplateVersion { get; private set; }
  345. }
  346. #>