EMIS.Entities.HRServices.Context.tt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF.Utility.CS.ttinclude"#><#@
  3. output extension=".cs"#><#
  4. var loader = new MetadataLoader(this);
  5. var region = new CodeRegion(this);
  6. var inputFile = @"HRServiceContext.edmx";
  7. var ItemCollection = loader.CreateEdmItemCollection(inputFile);
  8. Code = new CodeGenerationTools(this);
  9. EFTools = new MetadataTools(this);
  10. ObjectNamespace = Code.VsNamespaceSuggestion();
  11. ModelNamespace = loader.GetModelNamespace(inputFile);
  12. EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
  13. if (container == null)
  14. {
  15. return string.Empty;
  16. }
  17. #>
  18. //------------------------------------------------------------------------------
  19. // <auto-generated>
  20. // This code was generated from a template.
  21. //
  22. // Manual changes to this file may cause unexpected behavior in your application.
  23. // Manual changes to this file will be overwritten if the code is regenerated.
  24. // </auto-generated>
  25. //------------------------------------------------------------------------------
  26. #pragma warning disable 1573
  27. <#
  28. if (!String.IsNullOrEmpty(ObjectNamespace))
  29. {
  30. #>
  31. namespace <#=Code.EscapeNamespace(ObjectNamespace)#>
  32. {
  33. <#
  34. PushIndent(CodeRegion.GetIndent(1));
  35. }
  36. #>
  37. using System;
  38. using System.Data.Common;
  39. using System.Data.Entity;
  40. using System.Data.Entity.Infrastructure;
  41. <#
  42. if (container.FunctionImports.Any())
  43. {
  44. #>
  45. using System.Data.Objects;
  46. <#
  47. }
  48. #>
  49. <#=Accessibility.ForType(container)#> partial class <#=Code.Escape(container)#> : DbContext
  50. {
  51. static <#=Code.Escape(container)#>()
  52. {
  53. Database.SetInitializer<<#=Code.Escape(container)#>>(null);
  54. }
  55. public <#=Code.Escape(container)#>() : base("name=<#=container.Name#>") {
  56. var objectContext = (this as IObjectContextAdapter).ObjectContext;
  57. objectContext.CommandTimeout = 12000;
  58. base.Configuration.ProxyCreationEnabled = false;
  59. }
  60. public <#=Code.Escape(container)#>(string nameOrConnectionString) : base(nameOrConnectionString) {
  61. var objectContext = (this as IObjectContextAdapter).ObjectContext;
  62. objectContext.CommandTimeout = 12000;
  63. base.Configuration.ProxyCreationEnabled = false;
  64. }
  65. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  66. {
  67. modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
  68. <#
  69. foreach (EntityType entitySet in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  70. {
  71. #>
  72. modelBuilder.Configurations.Add(new <#=Code.Escape(entitySet.Name)#>_Mapping());
  73. <#
  74. }
  75. #>
  76. }
  77. <#
  78. foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
  79. {
  80. #>
  81. <#=Accessibility.ForReadOnlyProperty(entitySet)#> DbSet<<#=Code.Escape(entitySet.ElementType)#>> <#=Code.Escape(entitySet)#> { get; set; }
  82. <#
  83. }
  84. foreach (var edmFunction in container.FunctionImports)
  85. {
  86. WriteFunctionImport(edmFunction, false);
  87. }
  88. #>
  89. }
  90. <#
  91. if (!String.IsNullOrEmpty(ObjectNamespace))
  92. {
  93. PopIndent();
  94. #>
  95. }
  96. <#
  97. }
  98. #>
  99. <#+
  100. string ModelNamespace { get; set; }
  101. string ObjectNamespace { get; set; }
  102. CodeGenerationTools Code { get; set; }
  103. MetadataTools EFTools { get; set; }
  104. void WriteLazyLoadingEnabled(EntityContainer container)
  105. {
  106. string lazyLoadingAttributeValue = null;
  107. var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";
  108. if(MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue))
  109. {
  110. bool isLazyLoading;
  111. if(bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading) && !isLazyLoading)
  112. {
  113. #>
  114. this.Configuration.LazyLoadingEnabled = false;
  115. <#+
  116. }
  117. }
  118. }
  119. void WriteFunctionImport(EdmFunction edmFunction, bool includeMergeOption)
  120. {
  121. var parameters = FunctionImportParameter.Create(edmFunction.Parameters, Code, EFTools);
  122. var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
  123. var returnType = edmFunction.ReturnParameter == null ? null : EFTools.GetElementType(edmFunction.ReturnParameter.TypeUsage);
  124. var processedReturn = returnType == null ? "int" : "ObjectResult<" + MultiSchemaEscape(returnType) + ">";
  125. if (includeMergeOption)
  126. {
  127. paramList = Code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
  128. }
  129. #>
  130. <#=AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction))#> <#=processedReturn#> <#=Code.Escape(edmFunction)#>(<#=paramList#>)
  131. {
  132. <#+
  133. if(returnType != null && (returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType ||
  134. returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType))
  135. {
  136. #>
  137. ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(<#=MultiSchemaEscape(returnType)#>).Assembly);
  138. <#+
  139. }
  140. foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
  141. {
  142. var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
  143. var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
  144. var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
  145. #>
  146. var <#=parameter.LocalVariableName#> = <#=isNotNull#> ?
  147. <#=notNullInit#> :
  148. <#=nullInit#>;
  149. <#+
  150. }
  151. var genericArg = returnType == null ? "" : "<" + MultiSchemaEscape(returnType) + ">";
  152. var callParams = Code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
  153. if (includeMergeOption)
  154. {
  155. callParams = ", mergeOption" + callParams;
  156. }
  157. #>
  158. return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<#=genericArg#>("<#=edmFunction.Name#>"<#=callParams#>);
  159. }
  160. <#+
  161. if(!includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType)
  162. {
  163. WriteFunctionImport(edmFunction, true);
  164. }
  165. }
  166. string AccessibilityAndVirtual(string accessibility)
  167. {
  168. return accessibility + (accessibility != "private" ? " virtual" : "");
  169. }
  170. string MultiSchemaEscape(TypeUsage usage)
  171. {
  172. var type = usage.EdmType as StructuralType;
  173. return type != null && type.NamespaceName != ModelNamespace ?
  174. Code.CreateFullName(Code.EscapeNamespace(type.NamespaceName), Code.Escape(type)) :
  175. Code.Escape(usage);
  176. }
  177. #>