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