EMIS.Entities.Log.Context.tt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 = @"EMISLogContext.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. using EntityFramework.Extensions;
  42. using System.Linq.Expressions;
  43. using System.Data.Entity.Core.Objects;
  44. <#
  45. if (container.FunctionImports.Any())
  46. {
  47. #>
  48. using System.Data.Objects;
  49. <#
  50. }
  51. #>
  52. <#=Accessibility.ForType(container)#> partial class <#=Code.Escape(container)#> : DbContext
  53. {
  54. static <#=Code.Escape(container)#>()
  55. {
  56. Database.SetInitializer<<#=Code.Escape(container)#>>(null);
  57. }
  58. public <#=Code.Escape(container)#>() : base("name=<#=container.Name#>") {
  59. var objectContext = (this as IObjectContextAdapter).ObjectContext;
  60. objectContext.CommandTimeout = 12000;
  61. base.Configuration.ProxyCreationEnabled = false;
  62. }
  63. public <#=Code.Escape(container)#>(string nameOrConnectionString) : base(nameOrConnectionString) {
  64. var objectContext = (this as IObjectContextAdapter).ObjectContext;
  65. objectContext.CommandTimeout = 12000;
  66. base.Configuration.ProxyCreationEnabled = false;
  67. }
  68. protected void Delete<TEntity>(Expression<Func<TEntity, bool>> whereExpression) where TEntity : class
  69. {
  70. var context = ((IObjectContextAdapter)this).ObjectContext;
  71. IObjectSet<TEntity> source = context.CreateObjectSet<TEntity>();
  72. source.Delete(whereExpression);
  73. }
  74. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  75. {
  76. modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
  77. <#
  78. foreach (EntityType entitySet in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  79. {
  80. #>
  81. modelBuilder.Configurations.Add(new <#=Code.Escape(entitySet.Name)#>_Mapping());
  82. <#
  83. }
  84. #>
  85. }
  86. <#
  87. foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
  88. {
  89. #>
  90. <#=Accessibility.ForReadOnlyProperty(entitySet)#> DbSet<<#=Code.Escape(entitySet.ElementType)#>> <#=Code.Escape(entitySet)#> { get; set; }
  91. <#
  92. }
  93. foreach (var edmFunction in container.FunctionImports)
  94. {
  95. WriteFunctionImport(edmFunction, false);
  96. }
  97. #>
  98. }
  99. <#
  100. if (!String.IsNullOrEmpty(ObjectNamespace))
  101. {
  102. PopIndent();
  103. #>
  104. }
  105. <#
  106. }
  107. #>
  108. <#+
  109. string ModelNamespace { get; set; }
  110. string ObjectNamespace { get; set; }
  111. CodeGenerationTools Code { get; set; }
  112. MetadataTools EFTools { get; set; }
  113. void WriteLazyLoadingEnabled(EntityContainer container)
  114. {
  115. string lazyLoadingAttributeValue = null;
  116. var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";
  117. if(MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue))
  118. {
  119. bool isLazyLoading;
  120. if(bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading) && !isLazyLoading)
  121. {
  122. #>
  123. this.Configuration.LazyLoadingEnabled = false;
  124. <#+
  125. }
  126. }
  127. }
  128. void WriteFunctionImport(EdmFunction edmFunction, bool includeMergeOption)
  129. {
  130. var parameters = FunctionImportParameter.Create(edmFunction.Parameters, Code, EFTools);
  131. var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
  132. var returnType = edmFunction.ReturnParameter == null ? null : EFTools.GetElementType(edmFunction.ReturnParameter.TypeUsage);
  133. var processedReturn = returnType == null ? "int" : "ObjectResult<" + MultiSchemaEscape(returnType) + ">";
  134. if (includeMergeOption)
  135. {
  136. paramList = Code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
  137. }
  138. #>
  139. <#=AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction))#> <#=processedReturn#> <#=Code.Escape(edmFunction)#>(<#=paramList#>)
  140. {
  141. <#+
  142. if(returnType != null && (returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType ||
  143. returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType))
  144. {
  145. #>
  146. ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(<#=MultiSchemaEscape(returnType)#>).Assembly);
  147. <#+
  148. }
  149. foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
  150. {
  151. var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
  152. var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
  153. var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
  154. #>
  155. var <#=parameter.LocalVariableName#> = <#=isNotNull#> ?
  156. <#=notNullInit#> :
  157. <#=nullInit#>;
  158. <#+
  159. }
  160. var genericArg = returnType == null ? "" : "<" + MultiSchemaEscape(returnType) + ">";
  161. var callParams = Code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
  162. if (includeMergeOption)
  163. {
  164. callParams = ", mergeOption" + callParams;
  165. }
  166. #>
  167. return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<#=genericArg#>("<#=edmFunction.Name#>"<#=callParams#>);
  168. }
  169. <#+
  170. if(!includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType)
  171. {
  172. WriteFunctionImport(edmFunction, true);
  173. }
  174. }
  175. string AccessibilityAndVirtual(string accessibility)
  176. {
  177. return accessibility + (accessibility != "private" ? " virtual" : "");
  178. }
  179. string MultiSchemaEscape(TypeUsage usage)
  180. {
  181. var type = usage.EdmType as StructuralType;
  182. return type != null && type.NamespaceName != ModelNamespace ?
  183. Code.CreateFullName(Code.EscapeNamespace(type.NamespaceName), Code.Escape(type)) :
  184. Code.Escape(usage);
  185. }
  186. #>