Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CacheEntities.Context.tt 22KB

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