Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UserEntities.tt 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF6.Utility.CS.ttinclude"#><#@
  3. output extension=".cs"#><#
  4. const string inputFile = @"UserEntities.edmx";
  5. var textTransform = DynamicTextTransformation.Create(this);
  6. var code = new CodeGenerationTools(this);
  7. var ef = new MetadataTools(this);
  8. var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
  9. var fileManager = EntityFrameworkTemplateFileManager.Create(this);
  10. var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
  11. var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
  12. if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
  13. {
  14. return string.Empty;
  15. }
  16. WriteHeader(codeStringGenerator, fileManager);
  17. foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
  18. {
  19. fileManager.StartNewFile(entity.Name + ".cs");
  20. BeginNamespace(code);
  21. #>
  22. <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
  23. <#=codeStringGenerator.EntityClassOpening(entity)#>
  24. {
  25. <#
  26. var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
  27. var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
  28. var complexProperties = typeMapper.GetComplexProperties(entity);
  29. if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
  30. {
  31. #>
  32. public <#=code.Escape(entity)#>()
  33. {
  34. <#
  35. foreach (var edmProperty in propertiesWithDefaultValues)
  36. {
  37. #>
  38. this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
  39. <#
  40. }
  41. foreach (var navigationProperty in collectionNavigationProperties)
  42. {
  43. #>
  44. this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
  45. <#
  46. }
  47. foreach (var complexProperty in complexProperties)
  48. {
  49. #>
  50. this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
  51. <#
  52. }
  53. #>
  54. }
  55. <#
  56. }
  57. var simpleProperties = typeMapper.GetSimpleProperties(entity);
  58. if (simpleProperties.Any())
  59. {
  60. foreach (var edmProperty in simpleProperties)
  61. {
  62. #>
  63. <#=codeStringGenerator.Property(edmProperty)#>
  64. <#
  65. }
  66. }
  67. if (complexProperties.Any())
  68. {
  69. #>
  70. <#
  71. foreach(var complexProperty in complexProperties)
  72. {
  73. #>
  74. <#=codeStringGenerator.Property(complexProperty)#>
  75. <#
  76. }
  77. }
  78. var navigationProperties = typeMapper.GetNavigationProperties(entity);
  79. if (navigationProperties.Any())
  80. {
  81. #>
  82. <#
  83. foreach (var navigationProperty in navigationProperties)
  84. {
  85. #>
  86. <#=codeStringGenerator.NavigationProperty(navigationProperty)#>
  87. <#
  88. }
  89. }
  90. #>
  91. }
  92. <#
  93. EndNamespace(code);
  94. }
  95. foreach (var complex in typeMapper.GetItemsToGenerate<ComplexType>(itemCollection))
  96. {
  97. fileManager.StartNewFile(complex.Name + ".cs");
  98. BeginNamespace(code);
  99. #>
  100. <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
  101. <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
  102. {
  103. <#
  104. var complexProperties = typeMapper.GetComplexProperties(complex);
  105. var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex);
  106. if (propertiesWithDefaultValues.Any() || complexProperties.Any())
  107. {
  108. #>
  109. public <#=code.Escape(complex)#>()
  110. {
  111. <#
  112. foreach (var edmProperty in propertiesWithDefaultValues)
  113. {
  114. #>
  115. this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
  116. <#
  117. }
  118. foreach (var complexProperty in complexProperties)
  119. {
  120. #>
  121. this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
  122. <#
  123. }
  124. #>
  125. }
  126. <#
  127. }
  128. var simpleProperties = typeMapper.GetSimpleProperties(complex);
  129. if (simpleProperties.Any())
  130. {
  131. foreach(var edmProperty in simpleProperties)
  132. {
  133. #>
  134. <#=codeStringGenerator.Property(edmProperty)#>
  135. <#
  136. }
  137. }
  138. if (complexProperties.Any())
  139. {
  140. #>
  141. <#
  142. foreach(var edmProperty in complexProperties)
  143. {
  144. #>
  145. <#=codeStringGenerator.Property(edmProperty)#>
  146. <#
  147. }
  148. }
  149. #>
  150. }
  151. <#
  152. EndNamespace(code);
  153. }
  154. foreach (var enumType in typeMapper.GetEnumItemsToGenerate(itemCollection))
  155. {
  156. fileManager.StartNewFile(enumType.Name + ".cs");
  157. BeginNamespace(code);
  158. #>
  159. <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
  160. <#
  161. if (typeMapper.EnumIsFlags(enumType))
  162. {
  163. #>
  164. [Flags]
  165. <#
  166. }
  167. #>
  168. <#=codeStringGenerator.EnumOpening(enumType)#>
  169. {
  170. <#
  171. var foundOne = false;
  172. foreach (MetadataItem member in typeMapper.GetEnumMembers(enumType))
  173. {
  174. foundOne = true;
  175. #>
  176. <#=code.Escape(typeMapper.GetEnumMemberName(member))#> = <#=typeMapper.GetEnumMemberValue(member)#>,
  177. <#
  178. }
  179. if (foundOne)
  180. {
  181. this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1);
  182. }
  183. #>
  184. }
  185. <#
  186. EndNamespace(code);
  187. }
  188. fileManager.Process();
  189. #>
  190. <#+
  191. public void WriteHeader(CodeStringGenerator codeStringGenerator, EntityFrameworkTemplateFileManager fileManager)
  192. {
  193. fileManager.StartHeader();
  194. #>
  195. //------------------------------------------------------------------------------
  196. // <auto-generated>
  197. // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#>
  198. //
  199. // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#>
  200. // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#>
  201. // </auto-generated>
  202. //------------------------------------------------------------------------------
  203. <#=codeStringGenerator.UsingDirectives(inHeader: true)#>
  204. <#+
  205. fileManager.EndBlock();
  206. }
  207. public void BeginNamespace(CodeGenerationTools code)
  208. {
  209. var codeNamespace = code.VsNamespaceSuggestion();
  210. if (!String.IsNullOrEmpty(codeNamespace))
  211. {
  212. #>
  213. namespace <#=code.EscapeNamespace(codeNamespace)#>
  214. {
  215. <#+
  216. PushIndent(" ");
  217. }
  218. }
  219. public void EndNamespace(CodeGenerationTools code)
  220. {
  221. if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion()))
  222. {
  223. PopIndent();
  224. #>
  225. }
  226. <#+
  227. }
  228. }
  229. public const string TemplateId = "CSharp_DbContext_Types_EF6";
  230. public class CodeStringGenerator
  231. {
  232. private readonly CodeGenerationTools _code;
  233. private readonly TypeMapper _typeMapper;
  234. private readonly MetadataTools _ef;
  235. public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
  236. {
  237. ArgumentNotNull(code, "code");
  238. ArgumentNotNull(typeMapper, "typeMapper");
  239. ArgumentNotNull(ef, "ef");
  240. _code = code;
  241. _typeMapper = typeMapper;
  242. _ef = ef;
  243. }
  244. public string Property(EdmProperty edmProperty)
  245. {
  246. return string.Format(
  247. CultureInfo.InvariantCulture,
  248. "{0} {1} {2} {{ {3}get; {4}set; }}",
  249. Accessibility.ForProperty(edmProperty),
  250. _typeMapper.GetTypeName(edmProperty.TypeUsage),
  251. _code.Escape(edmProperty),
  252. _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
  253. _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  254. }
  255. public string NavigationProperty(NavigationProperty navProp)
  256. {
  257. var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType());
  258. return string.Format(
  259. CultureInfo.InvariantCulture,
  260. "{0} {1} {2} {{ {3}get; {4}set; }}",
  261. AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)),
  262. navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
  263. _code.Escape(navProp),
  264. _code.SpaceAfter(Accessibility.ForGetter(navProp)),
  265. _code.SpaceAfter(Accessibility.ForSetter(navProp)));
  266. }
  267. public string AccessibilityAndVirtual(string accessibility)
  268. {
  269. return accessibility + (accessibility != "private" ? " virtual" : "");
  270. }
  271. public string EntityClassOpening(EntityType entity)
  272. {
  273. return string.Format(
  274. CultureInfo.InvariantCulture,
  275. "{0} {1}partial class {2}{3}",
  276. Accessibility.ForType(entity),
  277. _code.SpaceAfter(_code.AbstractOption(entity)),
  278. _code.Escape(entity),
  279. _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
  280. }
  281. public string EnumOpening(SimpleType enumType)
  282. {
  283. return string.Format(
  284. CultureInfo.InvariantCulture,
  285. "{0} enum {1} : {2}",
  286. Accessibility.ForType(enumType),
  287. _code.Escape(enumType),
  288. _code.Escape(_typeMapper.UnderlyingClrType(enumType)));
  289. }
  290. public void WriteFunctionParameters(EdmFunction edmFunction, Action<string, string, string, string> writeParameter)
  291. {
  292. var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  293. foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
  294. {
  295. var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
  296. var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
  297. var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + TypeMapper.FixNamespaces(parameter.RawClrTypeName) + "))";
  298. writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
  299. }
  300. }
  301. public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
  302. {
  303. var parameters = _typeMapper.GetParameters(edmFunction);
  304. return string.Format(
  305. CultureInfo.InvariantCulture,
  306. "{0} IQueryable<{1}> {2}({3})",
  307. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  308. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  309. _code.Escape(edmFunction),
  310. string.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray()));
  311. }
  312. public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
  313. {
  314. var parameters = _typeMapper.GetParameters(edmFunction);
  315. return string.Format(
  316. CultureInfo.InvariantCulture,
  317. "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});",
  318. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  319. edmFunction.NamespaceName,
  320. edmFunction.Name,
  321. string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
  322. _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
  323. }
  324. public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  325. {
  326. var parameters = _typeMapper.GetParameters(edmFunction);
  327. var returnType = _typeMapper.GetReturnType(edmFunction);
  328. var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray());
  329. if (includeMergeOption)
  330. {
  331. paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
  332. }
  333. return string.Format(
  334. CultureInfo.InvariantCulture,
  335. "{0} {1} {2}({3})",
  336. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  337. returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  338. _code.Escape(edmFunction),
  339. paramList);
  340. }
  341. public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  342. {
  343. var parameters = _typeMapper.GetParameters(edmFunction);
  344. var returnType = _typeMapper.GetReturnType(edmFunction);
  345. var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
  346. if (includeMergeOption)
  347. {
  348. callParams = ", mergeOption" + callParams;
  349. }
  350. return string.Format(
  351. CultureInfo.InvariantCulture,
  352. "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
  353. returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  354. edmFunction.Name,
  355. callParams);
  356. }
  357. public string DbSet(EntitySet entitySet)
  358. {
  359. return string.Format(
  360. CultureInfo.InvariantCulture,
  361. "{0} virtual DbSet<{1}> {2} {{ get; set; }}",
  362. Accessibility.ForReadOnlyProperty(entitySet),
  363. _typeMapper.GetTypeName(entitySet.ElementType),
  364. _code.Escape(entitySet));
  365. }
  366. public string UsingDirectives(bool inHeader, bool includeCollections = true)
  367. {
  368. return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
  369. ? string.Format(
  370. CultureInfo.InvariantCulture,
  371. "{0}using System;{1}" +
  372. "{2}",
  373. inHeader ? Environment.NewLine : "",
  374. includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
  375. inHeader ? "" : Environment.NewLine)
  376. : "";
  377. }
  378. }
  379. public class TypeMapper
  380. {
  381. private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
  382. private readonly System.Collections.IList _errors;
  383. private readonly CodeGenerationTools _code;
  384. private readonly MetadataTools _ef;
  385. public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
  386. {
  387. ArgumentNotNull(code, "code");
  388. ArgumentNotNull(ef, "ef");
  389. ArgumentNotNull(errors, "errors");
  390. _code = code;
  391. _ef = ef;
  392. _errors = errors;
  393. }
  394. public static string FixNamespaces(string typeName)
  395. {
  396. return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial.");
  397. }
  398. public string GetTypeName(TypeUsage typeUsage)
  399. {
  400. return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
  401. }
  402. public string GetTypeName(EdmType edmType)
  403. {
  404. return GetTypeName(edmType, isNullable: null, modelNamespace: null);
  405. }
  406. public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
  407. {
  408. return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
  409. }
  410. public string GetTypeName(EdmType edmType, string modelNamespace)
  411. {
  412. return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
  413. }
  414. public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
  415. {
  416. if (edmType == null)
  417. {
  418. return null;
  419. }
  420. var collectionType = edmType as CollectionType;
  421. if (collectionType != null)
  422. {
  423. return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
  424. }
  425. var typeName = _code.Escape(edmType.MetadataProperties
  426. .Where(p => p.Name == ExternalTypeNameAttributeName)
  427. .Select(p => (string)p.Value)
  428. .FirstOrDefault())
  429. ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
  430. _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
  431. _code.Escape(edmType));
  432. if (edmType is StructuralType)
  433. {
  434. return typeName;
  435. }
  436. if (edmType is SimpleType)
  437. {
  438. var clrType = UnderlyingClrType(edmType);
  439. if (!IsEnumType(edmType))
  440. {
  441. typeName = _code.Escape(clrType);
  442. }
  443. typeName = FixNamespaces(typeName);
  444. return clrType.IsValueType && isNullable == true ?
  445. String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
  446. typeName;
  447. }
  448. throw new ArgumentException("edmType");
  449. }
  450. public Type UnderlyingClrType(EdmType edmType)
  451. {
  452. ArgumentNotNull(edmType, "edmType");
  453. var primitiveType = edmType as PrimitiveType;
  454. if (primitiveType != null)
  455. {
  456. return primitiveType.ClrEquivalentType;
  457. }
  458. if (IsEnumType(edmType))
  459. {
  460. return GetEnumUnderlyingType(edmType).ClrEquivalentType;
  461. }
  462. return typeof(object);
  463. }
  464. public object GetEnumMemberValue(MetadataItem enumMember)
  465. {
  466. ArgumentNotNull(enumMember, "enumMember");
  467. var valueProperty = enumMember.GetType().GetProperty("Value");
  468. return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
  469. }
  470. public string GetEnumMemberName(MetadataItem enumMember)
  471. {
  472. ArgumentNotNull(enumMember, "enumMember");
  473. var nameProperty = enumMember.GetType().GetProperty("Name");
  474. return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
  475. }
  476. public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
  477. {
  478. ArgumentNotNull(enumType, "enumType");
  479. var membersProperty = enumType.GetType().GetProperty("Members");
  480. return membersProperty != null
  481. ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
  482. : Enumerable.Empty<MetadataItem>();
  483. }
  484. public bool EnumIsFlags(EdmType enumType)
  485. {
  486. ArgumentNotNull(enumType, "enumType");
  487. var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
  488. return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
  489. }
  490. public bool IsEnumType(GlobalItem edmType)
  491. {
  492. ArgumentNotNull(edmType, "edmType");
  493. return edmType.GetType().Name == "EnumType";
  494. }
  495. public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
  496. {
  497. ArgumentNotNull(enumType, "enumType");
  498. return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
  499. }
  500. public string CreateLiteral(object value)
  501. {
  502. if (value == null || value.GetType() != typeof(TimeSpan))
  503. {
  504. return _code.CreateLiteral(value);
  505. }
  506. return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
  507. }
  508. public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
  509. {
  510. ArgumentNotNull(types, "types");
  511. ArgumentNotNull(sourceFile, "sourceFile");
  512. var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  513. if (types.Any(item => !hash.Add(item)))
  514. {
  515. _errors.Add(
  516. new CompilerError(sourceFile, -1, -1, "6023",
  517. String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict"))));
  518. return false;
  519. }
  520. return true;
  521. }
  522. public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
  523. {
  524. return GetItemsToGenerate<SimpleType>(itemCollection)
  525. .Where(e => IsEnumType(e));
  526. }
  527. public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
  528. {
  529. return itemCollection
  530. .OfType<T>()
  531. .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
  532. .OrderBy(i => i.Name);
  533. }
  534. public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
  535. {
  536. return itemCollection
  537. .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
  538. .Select(g => GetGlobalItemName(g));
  539. }
  540. public string GetGlobalItemName(GlobalItem item)
  541. {
  542. if (item is EdmType)
  543. {
  544. return ((EdmType)item).Name;
  545. }
  546. else
  547. {
  548. return ((EntityContainer)item).Name;
  549. }
  550. }
  551. public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
  552. {
  553. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  554. }
  555. public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
  556. {
  557. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  558. }
  559. public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
  560. {
  561. return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  562. }
  563. public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
  564. {
  565. return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  566. }
  567. public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
  568. {
  569. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  570. }
  571. public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
  572. {
  573. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  574. }
  575. public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
  576. {
  577. return type.NavigationProperties.Where(np => np.DeclaringType == type);
  578. }
  579. public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
  580. {
  581. return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  582. }
  583. public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
  584. {
  585. ArgumentNotNull(edmFunction, "edmFunction");
  586. var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
  587. return returnParamsProperty == null
  588. ? edmFunction.ReturnParameter
  589. : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
  590. }
  591. public bool IsComposable(EdmFunction edmFunction)
  592. {
  593. ArgumentNotNull(edmFunction, "edmFunction");
  594. var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
  595. return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
  596. }
  597. public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
  598. {
  599. return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  600. }
  601. public TypeUsage GetReturnType(EdmFunction edmFunction)
  602. {
  603. var returnParam = GetReturnParameter(edmFunction);
  604. return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
  605. }
  606. public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
  607. {
  608. var returnType = GetReturnType(edmFunction);
  609. return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
  610. }
  611. }
  612. public static void ArgumentNotNull<T>(T arg, string name) where T : class
  613. {
  614. if (arg == null)
  615. {
  616. throw new ArgumentNullException(name);
  617. }
  618. }
  619. #>