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