Model1.Context.tt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <#@ template language="VB" debug="false" hostspecific="true"#>
  2. <#@ include file="EF6.Utility.VB.ttinclude"#><#@
  3. output extension=".vb"#><#
  4. Const inputFile As String = "$edmxInputFile$"
  5. Dim textTransform As DynamicTextTransformation = DynamicTextTransformation.Create(Me)
  6. Dim code As CodeGenerationTools = New CodeGenerationTools(Me)
  7. Dim ef As MetadataTools = New MetadataTools(Me)
  8. Dim typeMapper As TypeMapper = New TypeMapper(code, ef, textTransform.Errors)
  9. Dim loader As EdmMetadataLoader = New EdmMetadataLoader(textTransform.Host, textTransform.Errors)
  10. Dim itemCollection As IEnumerable(Of GlobalItem) = loader.CreateEdmItemCollection(inputFile)
  11. Dim modelNamespace As String = loader.GetModelNamespace(inputFile)
  12. Dim codeStringGenerator As CodeStringGenerator = New CodeStringGenerator(code, typeMapper, ef)
  13. Dim container As EntityContainer = ItemCollection.OfType(Of EntityContainer)().FirstOrDefault()
  14. If container Is Nothing Then
  15. Return String.Empty
  16. End If
  17. #>
  18. '------------------------------------------------------------------------------
  19. ' <auto-generated>
  20. ' <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#>
  21. '
  22. ' <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#>
  23. ' <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#>
  24. ' </auto-generated>
  25. '------------------------------------------------------------------------------
  26. Imports System
  27. Imports System.Data.Entity
  28. Imports System.Data.Entity.Infrastructure
  29. <#
  30. If container.FunctionImports.Any()
  31. #>
  32. Imports System.Data.Entity.Core.Objects
  33. Imports System.Linq
  34. <#
  35. End If
  36. #>
  37. <#
  38. Dim codeNamespace As String = code.VsNamespaceSuggestion()
  39. If Not String.IsNullOrEmpty(codeNamespace) Then
  40. #>
  41. Namespace <#=code.EscapeNamespace(codeNamespace)#>
  42. <#
  43. PushIndent(" ")
  44. End If
  45. #>
  46. Partial <#=Accessibility.ForType(container)#> Class <#=code.Escape(container)#>
  47. Inherits DbContext
  48. Public Sub New()
  49. MyBase.New("name=<#=container.Name#>")
  50. <#
  51. If Not loader.IsLazyLoadingEnabled(container) Then
  52. #>
  53. MyBase.Configuration.LazyLoadingEnabled = False
  54. <#
  55. End If
  56. For Each entitySet As EntitySet In container.BaseEntitySets.OfType(Of EntitySet)()
  57. ''' Note: the DbSet members are defined below such that the getter and
  58. ''' setter always have the same accessibility as the DbSet definition
  59. If Not Accessibility.ForReadOnlyProperty(entitySet) = "Public"
  60. #>
  61. <#=codeStringGenerator.DbSetInitializer(entitySet)#>
  62. <#
  63. End If
  64. Next
  65. #>
  66. End Sub
  67. Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
  68. Throw New UnintentionalCodeFirstException()
  69. End Sub
  70. <#
  71. For Each entitySet As EntitySet In container.BaseEntitySets.OfType(Of EntitySet)()
  72. #>
  73. <#=codeStringGenerator.DbSet(entitySet)#>
  74. <#
  75. Next
  76. For Each edmFunction As EdmFunction In container.FunctionImports
  77. WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption := False)
  78. Next
  79. #>
  80. End Class
  81. <#
  82. If Not String.IsNullOrEmpty(codeNamespace) Then
  83. PopIndent()
  84. #>
  85. End Namespace
  86. <#
  87. End If
  88. #>
  89. <#+
  90. Public Sub WriteFunctionImport(typeMapper As TypeMapper, codeStringGenerator As CodeStringGenerator, edmFunction As EdmFunction, modelNamespace As String, includeMergeOption As Boolean)
  91. If typeMapper.IsComposable(edmFunction) Then
  92. #>
  93. <DbFunction("<#=edmFunction.NamespaceName#>", "<#=edmFunction.Name#>")>
  94. <#=codeStringGenerator.ComposableFunctionMethod(edmFunction, modelNamespace)#>
  95. <#+
  96. codeStringGenerator.WriteFunctionParameters(edmFunction, AddressOf WriteFunctionParameter)
  97. #>
  98. <#=codeStringGenerator.ComposableCreateQuery(edmFunction, modelNamespace)#>
  99. End Function
  100. <#+
  101. Else
  102. #>
  103. <#=codeStringGenerator.FunctionMethod(edmFunction, modelNamespace, includeMergeOption)#>
  104. <#+
  105. codeStringGenerator.WriteFunctionParameters(edmFunction, AddressOf WriteFunctionParameter)
  106. #>
  107. <#=codeStringGenerator.ExecuteFunction(edmFunction, modelNamespace, includeMergeOption)#>
  108. End Function
  109. <#+
  110. If typeMapper.GenerateMergeOptionFunction(edmFunction, includeMergeOption) Then
  111. WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption := True)
  112. End If
  113. End If
  114. End Sub
  115. Public Sub WriteFunctionParameter(name As String, isNotNull As String, notNullInit As String, nullInit As String)
  116. #>
  117. Dim <#=name#> As ObjectParameter = If(<#=isNotNull#>, <#=notNullInit#>, <#=nullInit#>)
  118. <#+
  119. End Sub
  120. Public Const TemplateId As String = "VB_DbContext_Context_EF6"
  121. Public Class CodeStringGenerator
  122. Private ReadOnly _code As CodeGenerationTools
  123. Private ReadOnly _typeMapper As TypeMapper
  124. Private ReadOnly _ef As MetadataTools
  125. Public Sub New(code As CodeGenerationTools, typeMapper As TypeMapper, ef As MetadataTools)
  126. ArgumentNotNull(code, "code")
  127. ArgumentNotNull(typeMapper, "typeMapper")
  128. ArgumentNotNull(ef, "ef")
  129. _code = code
  130. _typeMapper = typeMapper
  131. _ef = ef
  132. End Sub
  133. Public Function SimpleProperty(edmProperty As EdmProperty) As String
  134. Return SimpleOrComplexProperty(edmProperty, _code.StringBefore(" = ", _typeMapper.CreateLiteral(edmProperty.DefaultValue)))
  135. End Function
  136. Public Function ComplexProperty(edmProperty As EdmProperty) As String
  137. Return SimpleOrComplexProperty(edmProperty, " = New " & _typeMapper.GetTypeName(edmProperty.TypeUsage))
  138. End Function
  139. Public Function SimpleOrComplexProperty(edmProperty As EdmProperty, defaultValue As String) As String
  140. Return AnyProperty(
  141. Accessibility.ForProperty(edmProperty), _
  142. _typeMapper.GetTypeName(edmProperty.TypeUsage), _
  143. _code.Escape(edmProperty), _
  144. _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), _
  145. _code.SpaceAfter(Accessibility.ForSetter(edmProperty)), _
  146. defaultValue)
  147. End Function
  148. Public Function NavigationProperty(navProp As NavigationProperty) As String
  149. Dim endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType())
  150. Dim defaultValue = ""
  151. Dim propertyType = endType
  152. If(navProp.ToEndMember.RelationshipMultiplicity = RelationshipMultiplicity.Many)
  153. defaultValue = " = New HashSet(Of " & endType & ")"
  154. propertyType = "ICollection(Of " & endType & ")"
  155. End If
  156. Return AnyProperty(
  157. AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)), _
  158. propertyType, _
  159. _code.Escape(navProp), _
  160. _code.SpaceAfter(Accessibility.ForGetter(navProp)), _
  161. _code.SpaceAfter(Accessibility.ForSetter(navProp)), _
  162. defaultValue)
  163. End Function
  164. Public Function AccessibilityAndVirtual(accessibility As String) As String
  165. Return accessibility & (If(accessibility <> "Private", " Overridable", ""))
  166. End Function
  167. Public Function AnyProperty(accessibility As String, type As String, name As String, getterAccessibility As String, setterAccessibility As String, defaultValue As String)
  168. If (String.IsNullOrEmpty(getterAccessibility) AndAlso String.IsNullOrEmpty(setterAccessibility))
  169. Return String.Format( _
  170. CultureInfo.InvariantCulture, _
  171. " {0} Property {1} As {2}{3}", _
  172. accessibility, _
  173. name, _
  174. type, _
  175. defaultValue)
  176. Else
  177. Return String.Format( _
  178. CultureInfo.InvariantCulture, _
  179. "{6} Private _{0} As {1}{2}{6}" & _
  180. " {3} Property {0} As {1}{6}" & _
  181. " {4}Get{6}" & _
  182. " Return _{0}{6}" & _
  183. " End Get{6}" & _
  184. " {5}Set(ByVal value As {1}){6}" & _
  185. " _{0} = value{6}" & _
  186. " End Set{6}" & _
  187. " End Property", _
  188. name, _
  189. type, _
  190. defaultValue, _
  191. accessibility, _
  192. getterAccessibility, _
  193. setterAccessibility, _
  194. Environment.NewLine)
  195. End If
  196. End Function
  197. Public Function PropertyAccessibilityAndVirtual(ByVal member As EdmMember) As String
  198. Dim propertyAccess As String = Accessibility.ForProperty(member)
  199. Dim setAccess as String = Accessibility.ForSetter(member)
  200. Dim getAccess as String = Accessibility.ForGetter(member)
  201. If propertyAccess <> "Private" AndAlso setAccess <> "Private" AndAlso getAccess <> "Private" Then
  202. Return propertyAccess & " Overridable"
  203. End If
  204. Return propertyAccess
  205. End Function
  206. Public Function EntityClassOpening(entity As EntityType) As String
  207. Return String.Format( _
  208. CultureInfo.InvariantCulture, _
  209. "Partial {0} {1}Class {2}{3}", _
  210. Accessibility.ForType(entity), _
  211. _code.SpaceAfter(_code.MustInheritOption(entity)), _
  212. _code.Escape(entity), _
  213. _code.StringBefore(Environment.NewLine & " Inherits ", _typeMapper.GetTypeName(entity.BaseType)))
  214. End Function
  215. Public Function EnumOpening(enumType As SimpleType) As String
  216. Return String.Format( _
  217. CultureInfo.InvariantCulture, _
  218. "{0} Enum {1} As {2}", _
  219. Accessibility.ForType(enumType),
  220. _code.Escape(enumType),
  221. _code.Escape(_typeMapper.UnderlyingClrType(enumType)))
  222. End Function
  223. Public Sub WriteFunctionParameters(edmFunction As EdmFunction, writeParameter As Action(Of String, String, String, String))
  224. Dim parameters as IEnumerable(Of FunctionImportParameter) = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef)
  225. For Each parameter As FunctionImportParameter In parameters.Where(Function(p) p.NeedsLocalVariable)
  226. Dim isNotNull as String = If(parameter.IsNullableOfT, parameter.FunctionParameterName & ".HasValue", parameter.FunctionParameterName & " IsNot Nothing")
  227. Dim notNullInit as String = "New ObjectParameter(""" & parameter.EsqlParameterName & """, " & parameter.FunctionParameterName & ")"
  228. Dim nullInit as String = "New ObjectParameter(""" & parameter.EsqlParameterName & """, GetType(" & TypeMapper.FixNamespaces(parameter.RawClrTypeName) & "))"
  229. writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit)
  230. Next
  231. End Sub
  232. Public Function ComposableFunctionMethod(edmFunction As EdmFunction, modelNamespace As String) As String
  233. Dim parameters as IEnumerable(Of FunctionImportParameter) = _typeMapper.GetParameters(edmFunction)
  234. Return String.Format( _
  235. CultureInfo.InvariantCulture, _
  236. "{0} Function {1}({2}) As IQueryable(Of {3})", _
  237. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), _
  238. _code.Escape(edmFunction), _
  239. String.Join(", ", parameters.Select(Function(p) p.FunctionParameterName & " As " & TypeMapper.FixNamespaces(p.FunctionParameterType)).ToArray()), _
  240. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace))
  241. End Function
  242. Public Function ComposableCreateQuery(edmFunction As EdmFunction, modelNamespace As String) As String
  243. Dim parameters as IEnumerable(Of FunctionImportParameter) = _typeMapper.GetParameters(edmFunction)
  244. Return String.Format( _
  245. CultureInfo.InvariantCulture, _
  246. "Return DirectCast(Me, IObjectContextAdapter).ObjectContext.CreateQuery(Of {0})(""[{1}].[{2}]({3})""{4})", _
  247. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace), _
  248. edmFunction.NamespaceName, _
  249. edmFunction.Name, _
  250. String.Join(", ", parameters.Select(Function(p) "@" & Convert.ToString(p.EsqlParameterName)).ToArray()), _
  251. _code.StringBefore(", ", String.Join(", ", parameters.Select(Function(p) p.ExecuteParameterName).ToArray())))
  252. End Function
  253. Public Function FunctionMethod(edmFunction As EdmFunction, modelNamespace As String, includeMergeOption As Boolean) As String
  254. Dim parameters as IEnumerable(Of FunctionImportParameter) = _typeMapper.GetParameters(edmFunction)
  255. Dim returnType as TypeUsage = _typeMapper.GetReturnType(edmFunction)
  256. Dim paramList as String = String.Join(", ", parameters.Select(Function(p) p.FunctionParameterName & " As " & TypeMapper.FixNamespaces(p.FunctionParameterType)).ToArray())
  257. If includeMergeOption Then
  258. paramList = _code.StringAfter(paramList, ", ") & "mergeOption As MergeOption"
  259. End If
  260. Return String.Format( _
  261. CultureInfo.InvariantCulture, _
  262. "{0} Function {1}({2}) As {3}", _
  263. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)), _
  264. _code.Escape(edmFunction), _
  265. paramList, _
  266. If(returnType Is Nothing, "Integer", "ObjectResult(Of " & _typeMapper.GetTypeName(returnType, modelNamespace) & ")"))
  267. End Function
  268. Public Function ExecuteFunction(edmFunction As EdmFunction, modelNamespace As String, includeMergeOption As Boolean) As String
  269. Dim parameters as IEnumerable(Of FunctionImportParameter) = _typeMapper.GetParameters(edmFunction)
  270. Dim returnType as TypeUsage = _typeMapper.GetReturnType(edmFunction)
  271. Dim callParams as String = _code.StringBefore(", ", String.Join(", ", parameters.Select(Function(p) p.ExecuteParameterName).ToArray()))
  272. If includeMergeOption Then
  273. callParams = ", mergeOption" & callParams
  274. End If
  275. Return String.Format( _
  276. CultureInfo.InvariantCulture, _
  277. "Return DirectCast(Me, IObjectContextAdapter).ObjectContext.ExecuteFunction{0}(""{1}""{2})", _
  278. If(returnType Is Nothing, "", "(Of " & _typeMapper.GetTypeName(returnType, ModelNamespace) & ")"), _
  279. edmFunction.Name, _
  280. callParams)
  281. End Function
  282. Public Function DbSet(entitySet As EntitySet) As String
  283. Return String.Format( _
  284. CultureInfo.InvariantCulture, _
  285. "{0} Overridable Property {1}() As DbSet(Of {2})", _
  286. Accessibility.ForReadOnlyProperty(entitySet), _
  287. _code.Escape(entitySet), _
  288. _typeMapper.GetTypeName(entitySet.ElementType))
  289. End Function
  290. Public Function DbSetInitializer(entitySet As EntitySet) As String
  291. Return String.Format( _
  292. CultureInfo.InvariantCulture, _
  293. "{0} = Me.Set(Of {1})()", _
  294. _code.Escape(entitySet), _
  295. _typeMapper.GetTypeName(entitySet.ElementType))
  296. End Function
  297. End Class
  298. Public Class TypeMapper
  299. Private Const ExternalTypeNameAttributeName As String = "http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName"
  300. Private ReadOnly _errors As System.Collections.IList
  301. Private ReadOnly _code As CodeGenerationTools
  302. Private ReadOnly _ef As MetadataTools
  303. Public Sub New(code As CodeGenerationTools, ef As MetadataTools, errors As System.Collections.IList)
  304. ArgumentNotNull(code, "code")
  305. ArgumentNotNull(ef, "ef")
  306. ArgumentNotNull(errors, "errors")
  307. _code = code
  308. _ef = ef
  309. _errors = errors
  310. End Sub
  311. Public Shared Function FixNamespaces(typeName As String) As String
  312. Return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial.")
  313. End Function
  314. Public Function GetTypeName(typeUsage As TypeUsage) As String
  315. Return If(typeUsage Is Nothing, Nothing, GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace := Nothing))
  316. End Function
  317. Public Function GetTypeName(edmType As EdmType) As String
  318. Return GetTypeName(edmType, isNullable := Nothing, modelNamespace := Nothing)
  319. End Function
  320. Public Function GetTypeName(typeUsage As TypeUsage, modelNamespace As String) As String
  321. Return If(typeUsage Is Nothing, Nothing, GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace))
  322. End Function
  323. Public Function GetTypeName(edmType As EdmType, modelNamespace As String) As String
  324. Return GetTypeName(edmType, isNullable := Nothing, modelNamespace := modelNamespace)
  325. End Function
  326. Public Function GetTypeName(edmType As EdmType, isNullable As System.Nullable(Of Boolean), modelNamespace As String) As String
  327. If edmType Is Nothing Then
  328. Return Nothing
  329. End If
  330. Dim collectionType = TryCast(edmType, CollectionType)
  331. If collectionType IsNot Nothing Then
  332. Return String.Format(CultureInfo.InvariantCulture, "ICollection( Of {0})", GetTypeName(collectionType.TypeUsage, modelNamespace))
  333. End If
  334. Dim typeName = If(_code.Escape(edmType.MetadataProperties.Where(Function(p) p.Name = ExternalTypeNameAttributeName).Select(Function(p) DirectCast(p.Value, String)).FirstOrDefault()), (If(modelNamespace IsNot Nothing AndAlso edmType.NamespaceName <> modelNamespace, _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)), _code.Escape(edmType))))
  335. If TypeOf edmType Is StructuralType Then
  336. Return typeName
  337. End If
  338. If TypeOf edmType Is SimpleType Then
  339. Dim clrType = UnderlyingClrType(edmType)
  340. If Not (IsEnumType(edmType)) Then
  341. typeName = _code.Escape(clrType)
  342. End If
  343. typeName = FixNamespaces(typeName)
  344. Return If(clrType.IsValueType AndAlso isNullable = True, String.Format(CultureInfo.InvariantCulture, "Nullable(Of {0})", typeName), typeName)
  345. End If
  346. Throw New ArgumentException("edmType")
  347. End Function
  348. Public Function UnderlyingClrType(edmType As EdmType) As Type
  349. ArgumentNotNull(edmType, "edmType")
  350. Dim primitiveType as PrimitiveType = TryCast(edmType, PrimitiveType)
  351. If primitiveType IsNot Nothing Then
  352. Return primitiveType.ClrEquivalentType
  353. End If
  354. If IsEnumType(edmType) Then
  355. Return GetEnumUnderlyingType(edmType).ClrEquivalentType
  356. End If
  357. Return GetType(Object)
  358. End Function
  359. Public Function GetEnumMemberValue(enumMember As MetadataItem) As Object
  360. ArgumentNotNull(enumMember, "enumMember")
  361. Dim valueProperty As PropertyInfo = enumMember.GetType().GetProperty("Value")
  362. Return If(valueProperty Is Nothing, Nothing, valueProperty.GetValue(enumMember, Nothing))
  363. End Function
  364. Public Function GetEnumMemberName(enumMember As MetadataItem) As String
  365. ArgumentNotNull(enumMember, "enumMember")
  366. Dim nameProperty As PropertyInfo = enumMember.GetType().GetProperty("Name")
  367. Return If(nameProperty Is Nothing, Nothing, DirectCast(nameProperty.GetValue(enumMember, Nothing), String))
  368. End Function
  369. Public Function GetEnumMembers(enumType As EdmType) As System.Collections.IEnumerable
  370. ArgumentNotNull(enumType, "enumType")
  371. Dim membersProperty As PropertyInfo = enumType.GetType().GetProperty("Members")
  372. Return If(membersProperty IsNot Nothing, DirectCast(membersProperty.GetValue(enumType, Nothing), System.Collections.IEnumerable), Enumerable.Empty(Of MetadataItem)())
  373. End Function
  374. Public Function EnumIsFlags(enumType As EdmType) As Boolean
  375. ArgumentNotNull(enumType, "enumType")
  376. Dim isFlagsProperty As PropertyInfo = enumType.GetType().GetProperty("IsFlags")
  377. Return isFlagsProperty IsNot Nothing AndAlso CBool(isFlagsProperty.GetValue(enumType, Nothing))
  378. End Function
  379. Public Function IsEnumType(edmType As GlobalItem) As Boolean
  380. ArgumentNotNull(edmType, "edmType")
  381. Return edmType.GetType().Name = "EnumType"
  382. End Function
  383. Public Function GetEnumUnderlyingType(enumType As EdmType) As PrimitiveType
  384. ArgumentNotNull(enumType, "enumType")
  385. Return DirectCast(enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, Nothing), PrimitiveType)
  386. End Function
  387. Public Function CreateLiteral(value As Object) As String
  388. If value Is Nothing OrElse value.GetType() IsNot GetType(TimeSpan) Then
  389. Return _code.CreateLiteral(value)
  390. End If
  391. Return String.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", DirectCast(value, TimeSpan).Ticks)
  392. End Function
  393. Public Function VerifyCaseInsensitiveTypeUniqueness(types As IEnumerable(Of String), sourceFile As String) As Boolean
  394. ArgumentNotNull(types, "types")
  395. ArgumentNotNull(sourceFile, "sourceFile")
  396. Dim hash As HashSet(Of String) = New HashSet(Of String)(StringComparer.InvariantCultureIgnoreCase)
  397. If types.Any(Function(item) Not hash.Add(item)) Then
  398. _errors.Add(New CompilerError(sourceFile, -1, -1, "6023", String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict"))))
  399. Return False
  400. End If
  401. Return True
  402. End Function
  403. Public Function GetEnumItemsToGenerate(itemCollection As IEnumerable(Of GlobalItem)) As IEnumerable(Of SimpleType)
  404. Return GetItemsToGenerate(Of SimpleType)(itemCollection).Where(Function(e) IsEnumType(e))
  405. End Function
  406. Public Function GetItemsToGenerate(Of T As EdmType)(itemCollection As IEnumerable(Of GlobalItem)) As IEnumerable(Of T)
  407. Return itemCollection.OfType(Of T)().Where(Function(i) Not i.MetadataProperties.Any(Function(p) p.Name = ExternalTypeNameAttributeName)).OrderBy(Function(i) i.Name)
  408. End Function
  409. Public Function GetAllGlobalItems(itemCollection As IEnumerable(Of GlobalItem)) As IEnumerable(Of String)
  410. Return itemCollection.Where(Function(i) TypeOf i Is EntityType OrElse TypeOf i Is ComplexType OrElse TypeOf i Is EntityContainer OrElse IsEnumType(i)).Select(Function(g) GetGlobalItemName(g))
  411. End Function
  412. Public Function GetGlobalItemName(item As GlobalItem) As String
  413. If TypeOf item Is EdmType Then
  414. Return DirectCast(item, EdmType).Name
  415. Else
  416. Return DirectCast(item, EntityContainer).Name
  417. End If
  418. End Function
  419. Public Function GetSimpleProperties(type As EntityType) As IEnumerable(Of EdmProperty)
  420. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is SimpleType AndAlso p.DeclaringType.Equals(type))
  421. End Function
  422. Public Function GetSimpleProperties(type As ComplexType) As IEnumerable(Of EdmProperty)
  423. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is SimpleType AndAlso p.DeclaringType.Equals(type))
  424. End Function
  425. Public Function GetComplexProperties(type As EntityType) As IEnumerable(Of EdmProperty)
  426. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is ComplexType AndAlso p.DeclaringType.Equals(type))
  427. End Function
  428. Public Function GetComplexProperties(type As ComplexType) As IEnumerable(Of EdmProperty)
  429. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is ComplexType AndAlso p.DeclaringType.Equals(type))
  430. End Function
  431. Public Function GetPropertiesWithDefaultValues(type As EntityType) As IEnumerable(Of EdmProperty)
  432. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is SimpleType AndAlso p.DeclaringType.Equals(type) AndAlso p.DefaultValue IsNot Nothing)
  433. End Function
  434. Public Function GetPropertiesWithDefaultValues(type As ComplexType) As IEnumerable(Of EdmProperty)
  435. Return type.Properties.Where(Function(p) TypeOf p.TypeUsage.EdmType Is SimpleType AndAlso p.DeclaringType.Equals(type) AndAlso p.DefaultValue IsNot Nothing)
  436. End Function
  437. Public Function GetNavigationProperties(type As EntityType) As IEnumerable(Of NavigationProperty)
  438. Return type.NavigationProperties.Where(Function(np) np.DeclaringType.Equals(type))
  439. End Function
  440. Public Function GetCollectionNavigationProperties(type As EntityType) As IEnumerable(Of NavigationProperty)
  441. Return type.NavigationProperties.Where(Function(np) np.DeclaringType.Equals(type) AndAlso np.ToEndMember.RelationshipMultiplicity = RelationshipMultiplicity.Many)
  442. End Function
  443. Public Function GetReturnParameter(edmFunction As EdmFunction) As FunctionParameter
  444. ArgumentNotNull(edmFunction, "edmFunction")
  445. Dim returnParamsProperty As PropertyInfo = edmFunction.GetType().GetProperty("ReturnParameters")
  446. Return If(returnParamsProperty Is Nothing, edmFunction.ReturnParameter, DirectCast(returnParamsProperty.GetValue(edmFunction, Nothing), IEnumerable(Of FunctionParameter)).FirstOrDefault())
  447. End Function
  448. Public Function IsComposable(edmFunction As EdmFunction) As Boolean
  449. ArgumentNotNull(edmFunction, "edmFunction")
  450. Dim isComposableProperty As PropertyInfo = edmFunction.GetType().GetProperty("IsComposableAttribute")
  451. Return isComposableProperty IsNot Nothing AndAlso CBool(isComposableProperty.GetValue(edmFunction, Nothing))
  452. End Function
  453. Public Function GetParameters(edmFunction As EdmFunction) As IEnumerable(Of FunctionImportParameter)
  454. Return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef)
  455. End Function
  456. Public Function GetReturnType(edmFunction As EdmFunction) As TypeUsage
  457. Dim returnParam As FunctionParameter = GetReturnParameter(edmFunction)
  458. Return If(returnParam Is Nothing, Nothing, _ef.GetElementType(returnParam.TypeUsage))
  459. End Function
  460. Public Function GenerateMergeOptionFunction(edmFunction As EdmFunction, includeMergeOption As Boolean) As Boolean
  461. Dim returnType As TypeUsage = GetReturnType(edmFunction)
  462. Return Not includeMergeOption AndAlso returnType IsNot Nothing AndAlso returnType.EdmType.BuiltInTypeKind = BuiltInTypeKind.EntityType
  463. End Function
  464. End Class
  465. Public Shared Sub ArgumentNotNull(Of T As Class)(arg As T, name As String)
  466. If arg Is Nothing Then
  467. Throw New ArgumentNullException(name)
  468. End If
  469. End Sub
  470. #>