DataCenterContext.Context.tt 22 KB

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