1 <#@ template language="C#" debug="false" hostspecific="true"#>
2 <#@ include file="EF6.Utility.CS.ttinclude"#><#@
3 output extension=".cs"#><#
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 fileManager = EntityFrameworkTemplateFileManager.Create(this);
11 var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
12 var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
14 if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
19 WriteHeader(codeStringGenerator, fileManager);
21 foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
23 fileManager.StartNewFile(entity.Name + ".cs");
26 <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
27 <#=codeStringGenerator.EntityClassOpening(entity)#>
30 var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
31 var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
32 var complexProperties = typeMapper.GetComplexProperties(entity);
34 if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
37 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
38 public <#=code.Escape(entity)#>()
41 foreach (var edmProperty in propertiesWithDefaultValues)
44 this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
48 foreach (var navigationProperty in collectionNavigationProperties)
51 this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
55 foreach (var complexProperty in complexProperties)
58 this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
67 var simpleProperties = typeMapper.GetSimpleProperties(entity);
68 if (simpleProperties.Any())
70 foreach (var edmProperty in simpleProperties)
73 <#=codeStringGenerator.Property(edmProperty)#>
78 if (complexProperties.Any())
83 foreach(var complexProperty in complexProperties)
86 <#=codeStringGenerator.Property(complexProperty)#>
91 var navigationProperties = typeMapper.GetNavigationProperties(entity);
92 if (navigationProperties.Any())
97 foreach (var navigationProperty in navigationProperties)
99 if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
102 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
106 <#=codeStringGenerator.NavigationProperty(navigationProperty)#>
116 foreach (var complex in typeMapper.GetItemsToGenerate<ComplexType>(itemCollection))
118 fileManager.StartNewFile(complex.Name + ".cs");
119 BeginNamespace(code);
121 <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
122 <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
125 var complexProperties = typeMapper.GetComplexProperties(complex);
126 var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex);
128 if (propertiesWithDefaultValues.Any() || complexProperties.Any())
131 public <#=code.Escape(complex)#>()
134 foreach (var edmProperty in propertiesWithDefaultValues)
137 this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
141 foreach (var complexProperty in complexProperties)
144 this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
153 var simpleProperties = typeMapper.GetSimpleProperties(complex);
154 if (simpleProperties.Any())
156 foreach(var edmProperty in simpleProperties)
159 <#=codeStringGenerator.Property(edmProperty)#>
164 if (complexProperties.Any())
169 foreach(var edmProperty in complexProperties)
172 <#=codeStringGenerator.Property(edmProperty)#>
182 foreach (var enumType in typeMapper.GetEnumItemsToGenerate(itemCollection))
184 fileManager.StartNewFile(enumType.Name + ".cs");
185 BeginNamespace(code);
187 <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
189 if (typeMapper.EnumIsFlags(enumType))
196 <#=codeStringGenerator.EnumOpening(enumType)#>
199 var foundOne = false;
201 foreach (MetadataItem member in typeMapper.GetEnumMembers(enumType))
205 <#=code.Escape(typeMapper.GetEnumMemberName(member))#> = <#=typeMapper.GetEnumMemberValue(member)#>,
211 this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1);
219 fileManager.Process();
224 public void WriteHeader(CodeStringGenerator codeStringGenerator, EntityFrameworkTemplateFileManager fileManager)
226 fileManager.StartHeader();
228 //------------------------------------------------------------------------------
230 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#>
232 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#>
233 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#>
235 //------------------------------------------------------------------------------
236 <#=codeStringGenerator.UsingDirectives(inHeader: true)#>
238 fileManager.EndBlock();
241 public void BeginNamespace(CodeGenerationTools code)
243 var codeNamespace = code.VsNamespaceSuggestion();
244 if (!String.IsNullOrEmpty(codeNamespace))
247 namespace <#=code.EscapeNamespace(codeNamespace)#>
254 public void EndNamespace(CodeGenerationTools code)
256 if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion()))
265 public const string TemplateId = "CSharp_DbContext_Types_EF6";
267 public class CodeStringGenerator
269 private readonly CodeGenerationTools _code;
270 private readonly TypeMapper _typeMapper;
271 private readonly MetadataTools _ef;
273 public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
275 ArgumentNotNull(code, "code");
276 ArgumentNotNull(typeMapper, "typeMapper");
277 ArgumentNotNull(ef, "ef");
280 _typeMapper = typeMapper;
284 public string Property(EdmProperty edmProperty)
286 return string.Format(
287 CultureInfo.InvariantCulture,
288 "{0} {1} {2} {{ {3}get; {4}set; }}",
289 Accessibility.ForProperty(edmProperty),
290 _typeMapper.GetTypeName(edmProperty.TypeUsage),
291 _code.Escape(edmProperty),
292 _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
293 _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
296 public string NavigationProperty(NavigationProperty navProp)
298 var endType = _typeMapper.GetTypeName(navProp.ToEndMember.GetEntityType());
299 return string.Format(
300 CultureInfo.InvariantCulture,
301 "{0} {1} {2} {{ {3}get; {4}set; }}",
302 AccessibilityAndVirtual(Accessibility.ForNavigationProperty(navProp)),
303 navProp.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
304 _code.Escape(navProp),
305 _code.SpaceAfter(Accessibility.ForGetter(navProp)),
306 _code.SpaceAfter(Accessibility.ForSetter(navProp)));
309 public string AccessibilityAndVirtual(string accessibility)
311 return accessibility + (accessibility != "private" ? " virtual" : "");
314 public string EntityClassOpening(EntityType entity)
316 return string.Format(
317 CultureInfo.InvariantCulture,
318 "{0} {1}partial class {2}{3}",
319 Accessibility.ForType(entity),
320 _code.SpaceAfter(_code.AbstractOption(entity)),
321 _code.Escape(entity),
322 _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
325 public string EnumOpening(SimpleType enumType)
327 return string.Format(
328 CultureInfo.InvariantCulture,
329 "{0} enum {1} : {2}",
330 Accessibility.ForType(enumType),
331 _code.Escape(enumType),
332 _code.Escape(_typeMapper.UnderlyingClrType(enumType)));
335 public void WriteFunctionParameters(EdmFunction edmFunction, Action<string, string, string, string> writeParameter)
337 var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
338 foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
340 var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
341 var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
342 var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + TypeMapper.FixNamespaces(parameter.RawClrTypeName) + "))";
343 writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
347 public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
349 var parameters = _typeMapper.GetParameters(edmFunction);
351 return string.Format(
352 CultureInfo.InvariantCulture,
353 "{0} IQueryable<{1}> {2}({3})",
354 AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
355 _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
356 _code.Escape(edmFunction),
357 string.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray()));
360 public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
362 var parameters = _typeMapper.GetParameters(edmFunction);
364 return string.Format(
365 CultureInfo.InvariantCulture,
366 "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});",
367 _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
368 edmFunction.NamespaceName,
370 string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
371 _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
374 public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
376 var parameters = _typeMapper.GetParameters(edmFunction);
377 var returnType = _typeMapper.GetReturnType(edmFunction);
379 var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray());
380 if (includeMergeOption)
382 paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
385 return string.Format(
386 CultureInfo.InvariantCulture,
388 AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
389 returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
390 _code.Escape(edmFunction),
394 public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
396 var parameters = _typeMapper.GetParameters(edmFunction);
397 var returnType = _typeMapper.GetReturnType(edmFunction);
399 var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
400 if (includeMergeOption)
402 callParams = ", mergeOption" + callParams;
405 return string.Format(
406 CultureInfo.InvariantCulture,
407 "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
408 returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
413 public string DbSet(EntitySet entitySet)
415 return string.Format(
416 CultureInfo.InvariantCulture,
417 "{0} virtual DbSet<{1}> {2} {{ get; set; }}",
418 Accessibility.ForReadOnlyProperty(entitySet),
419 _typeMapper.GetTypeName(entitySet.ElementType),
420 _code.Escape(entitySet));
423 public string UsingDirectives(bool inHeader, bool includeCollections = true)
425 return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
427 CultureInfo.InvariantCulture,
428 "{0}using System;{1}" +
430 inHeader ? Environment.NewLine : "",
431 includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
432 inHeader ? "" : Environment.NewLine)
437 public class TypeMapper
439 private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
441 private readonly System.Collections.IList _errors;
442 private readonly CodeGenerationTools _code;
443 private readonly MetadataTools _ef;
445 public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
447 ArgumentNotNull(code, "code");
448 ArgumentNotNull(ef, "ef");
449 ArgumentNotNull(errors, "errors");
456 public static string FixNamespaces(string typeName)
458 return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial.");
461 public string GetTypeName(TypeUsage typeUsage)
463 return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
466 public string GetTypeName(EdmType edmType)
468 return GetTypeName(edmType, isNullable: null, modelNamespace: null);
471 public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
473 return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
476 public string GetTypeName(EdmType edmType, string modelNamespace)
478 return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
481 public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
488 var collectionType = edmType as CollectionType;
489 if (collectionType != null)
491 return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
494 var typeName = _code.Escape(edmType.MetadataProperties
495 .Where(p => p.Name == ExternalTypeNameAttributeName)
496 .Select(p => (string)p.Value)
498 ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
499 _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
500 _code.Escape(edmType));
502 if (edmType is StructuralType)
507 if (edmType is SimpleType)
509 var clrType = UnderlyingClrType(edmType);
510 if (!IsEnumType(edmType))
512 typeName = _code.Escape(clrType);
515 typeName = FixNamespaces(typeName);
517 return clrType.IsValueType && isNullable == true ?
518 String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
522 throw new ArgumentException("edmType");
525 public Type UnderlyingClrType(EdmType edmType)
527 ArgumentNotNull(edmType, "edmType");
529 var primitiveType = edmType as PrimitiveType;
530 if (primitiveType != null)
532 return primitiveType.ClrEquivalentType;
535 if (IsEnumType(edmType))
537 return GetEnumUnderlyingType(edmType).ClrEquivalentType;
540 return typeof(object);
543 public object GetEnumMemberValue(MetadataItem enumMember)
545 ArgumentNotNull(enumMember, "enumMember");
547 var valueProperty = enumMember.GetType().GetProperty("Value");
548 return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
551 public string GetEnumMemberName(MetadataItem enumMember)
553 ArgumentNotNull(enumMember, "enumMember");
555 var nameProperty = enumMember.GetType().GetProperty("Name");
556 return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
559 public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
561 ArgumentNotNull(enumType, "enumType");
563 var membersProperty = enumType.GetType().GetProperty("Members");
564 return membersProperty != null
565 ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
566 : Enumerable.Empty<MetadataItem>();
569 public bool EnumIsFlags(EdmType enumType)
571 ArgumentNotNull(enumType, "enumType");
573 var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
574 return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
577 public bool IsEnumType(GlobalItem edmType)
579 ArgumentNotNull(edmType, "edmType");
581 return edmType.GetType().Name == "EnumType";
584 public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
586 ArgumentNotNull(enumType, "enumType");
588 return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
591 public string CreateLiteral(object value)
593 if (value == null || value.GetType() != typeof(TimeSpan))
595 return _code.CreateLiteral(value);
598 return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
601 public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
603 ArgumentNotNull(types, "types");
604 ArgumentNotNull(sourceFile, "sourceFile");
606 var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
607 if (types.Any(item => !hash.Add(item)))
610 new CompilerError(sourceFile, -1, -1, "6023",
611 String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict"))));
617 public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
619 return GetItemsToGenerate<SimpleType>(itemCollection)
620 .Where(e => IsEnumType(e));
623 public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
625 return itemCollection
627 .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
628 .OrderBy(i => i.Name);
631 public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
633 return itemCollection
634 .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
635 .Select(g => GetGlobalItemName(g));
638 public string GetGlobalItemName(GlobalItem item)
642 return ((EdmType)item).Name;
646 return ((EntityContainer)item).Name;
650 public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
652 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
655 public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
657 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
660 public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
662 return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
665 public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
667 return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
670 public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
672 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
675 public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
677 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
680 public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
682 return type.NavigationProperties.Where(np => np.DeclaringType == type);
685 public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
687 return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
690 public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
692 ArgumentNotNull(edmFunction, "edmFunction");
694 var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
695 return returnParamsProperty == null
696 ? edmFunction.ReturnParameter
697 : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
700 public bool IsComposable(EdmFunction edmFunction)
702 ArgumentNotNull(edmFunction, "edmFunction");
704 var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
705 return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
708 public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
710 return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
713 public TypeUsage GetReturnType(EdmFunction edmFunction)
715 var returnParam = GetReturnParameter(edmFunction);
716 return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
719 public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
721 var returnType = GetReturnType(edmFunction);
722 return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
726 public static void ArgumentNotNull<T>(T arg, string name) where T : class
730 throw new ArgumentNullException(name);