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 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);
15 var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
16 if (container == null)
21 //------------------------------------------------------------------------------
23 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#>
25 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#>
26 // <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#>
28 //------------------------------------------------------------------------------
32 var codeNamespace = code.VsNamespaceSuggestion();
33 if (!String.IsNullOrEmpty(codeNamespace))
36 namespace <#=code.EscapeNamespace(codeNamespace)#>
44 using System.Data.Entity;
45 using System.Data.Entity.Infrastructure;
47 if (container.FunctionImports.Any())
50 using System.Data.Entity.Core.Objects;
56 <#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
58 public <#=code.Escape(container)#>()
59 : base("name=<#=container.Name#>")
62 if (!loader.IsLazyLoadingEnabled(container))
65 this.Configuration.LazyLoadingEnabled = false;
69 foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
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")
76 <#=codeStringGenerator.DbSetInitializer(entitySet)#>
83 protected override void OnModelCreating(DbModelBuilder modelBuilder)
85 throw new UnintentionalCodeFirstException();
89 foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
92 <#=codeStringGenerator.DbSet(entitySet)#>
96 foreach (var edmFunction in container.FunctionImports)
98 WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: false);
104 if (!String.IsNullOrEmpty(codeNamespace))
114 private void WriteFunctionImport(TypeMapper typeMapper, CodeStringGenerator codeStringGenerator, EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
116 if (typeMapper.IsComposable(edmFunction))
120 [DbFunction("<#=edmFunction.NamespaceName#>", "<#=edmFunction.Name#>")]
121 <#=codeStringGenerator.ComposableFunctionMethod(edmFunction, modelNamespace)#>
124 codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter);
126 <#=codeStringGenerator.ComposableCreateQuery(edmFunction, modelNamespace)#>
134 <#=codeStringGenerator.FunctionMethod(edmFunction, modelNamespace, includeMergeOption)#>
137 codeStringGenerator.WriteFunctionParameters(edmFunction, WriteFunctionParameter);
139 <#=codeStringGenerator.ExecuteFunction(edmFunction, modelNamespace, includeMergeOption)#>
142 if (typeMapper.GenerateMergeOptionFunction(edmFunction, includeMergeOption))
144 WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: true);
149 public void WriteFunctionParameter(string name, string isNotNull, string notNullInit, string nullInit)
152 var <#=name#> = <#=isNotNull#> ?
159 public const string TemplateId = "CSharp_DbContext_Context_EF6";
161 public class CodeStringGenerator
163 private readonly CodeGenerationTools _code;
164 private readonly TypeMapper _typeMapper;
165 private readonly MetadataTools _ef;
167 public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
169 ArgumentNotNull(code, "code");
170 ArgumentNotNull(typeMapper, "typeMapper");
171 ArgumentNotNull(ef, "ef");
174 _typeMapper = typeMapper;
178 public string Property(EdmProperty edmProperty)
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)));
190 public string NavigationProperty(NavigationProperty navProp)
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)));
203 public string AccessibilityAndVirtual(string accessibility)
205 return accessibility + (accessibility != "private" ? " virtual" : "");
208 public string EntityClassOpening(EntityType entity)
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)));
219 public string EnumOpening(SimpleType enumType)
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)));
229 public void WriteFunctionParameters(EdmFunction edmFunction, Action<string, string, string, string> writeParameter)
231 var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
232 foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
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);
241 public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
243 var parameters = _typeMapper.GetParameters(edmFunction);
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()));
254 public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
256 var parameters = _typeMapper.GetParameters(edmFunction);
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,
264 string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
265 _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
268 public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
270 var parameters = _typeMapper.GetParameters(edmFunction);
271 var returnType = _typeMapper.GetReturnType(edmFunction);
273 var paramList = String.Join(", ", parameters.Select(p => TypeMapper.FixNamespaces(p.FunctionParameterType) + " " + p.FunctionParameterName).ToArray());
274 if (includeMergeOption)
276 paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
279 return string.Format(
280 CultureInfo.InvariantCulture,
282 AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
283 returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
284 _code.Escape(edmFunction),
288 public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
290 var parameters = _typeMapper.GetParameters(edmFunction);
291 var returnType = _typeMapper.GetReturnType(edmFunction);
293 var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
294 if (includeMergeOption)
296 callParams = ", mergeOption" + callParams;
299 return string.Format(
300 CultureInfo.InvariantCulture,
301 "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
302 returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
307 public string DbSet(EntitySet entitySet)
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));
317 public string DbSetInitializer(EntitySet entitySet)
319 return string.Format(
320 CultureInfo.InvariantCulture,
322 _code.Escape(entitySet),
323 _typeMapper.GetTypeName(entitySet.ElementType));
326 public string UsingDirectives(bool inHeader, bool includeCollections = true)
328 return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
330 CultureInfo.InvariantCulture,
331 "{0}using System;{1}" +
333 inHeader ? Environment.NewLine : "",
334 includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
335 inHeader ? "" : Environment.NewLine)
340 public class TypeMapper
342 private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
344 private readonly System.Collections.IList _errors;
345 private readonly CodeGenerationTools _code;
346 private readonly MetadataTools _ef;
348 public static string FixNamespaces(string typeName)
350 return typeName.Replace("System.Data.Spatial.", "System.Data.Entity.Spatial.");
353 public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
355 ArgumentNotNull(code, "code");
356 ArgumentNotNull(ef, "ef");
357 ArgumentNotNull(errors, "errors");
364 public string GetTypeName(TypeUsage typeUsage)
366 return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
369 public string GetTypeName(EdmType edmType)
371 return GetTypeName(edmType, isNullable: null, modelNamespace: null);
374 public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
376 return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
379 public string GetTypeName(EdmType edmType, string modelNamespace)
381 return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
384 public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
391 var collectionType = edmType as CollectionType;
392 if (collectionType != null)
394 return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
397 var typeName = _code.Escape(edmType.MetadataProperties
398 .Where(p => p.Name == ExternalTypeNameAttributeName)
399 .Select(p => (string)p.Value)
401 ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
402 _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
403 _code.Escape(edmType));
405 if (edmType is StructuralType)
410 if (edmType is SimpleType)
412 var clrType = UnderlyingClrType(edmType);
413 if (!IsEnumType(edmType))
415 typeName = _code.Escape(clrType);
418 typeName = FixNamespaces(typeName);
420 return clrType.IsValueType && isNullable == true ?
421 String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
425 throw new ArgumentException("edmType");
428 public Type UnderlyingClrType(EdmType edmType)
430 ArgumentNotNull(edmType, "edmType");
432 var primitiveType = edmType as PrimitiveType;
433 if (primitiveType != null)
435 return primitiveType.ClrEquivalentType;
438 if (IsEnumType(edmType))
440 return GetEnumUnderlyingType(edmType).ClrEquivalentType;
443 return typeof(object);
446 public object GetEnumMemberValue(MetadataItem enumMember)
448 ArgumentNotNull(enumMember, "enumMember");
450 var valueProperty = enumMember.GetType().GetProperty("Value");
451 return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
454 public string GetEnumMemberName(MetadataItem enumMember)
456 ArgumentNotNull(enumMember, "enumMember");
458 var nameProperty = enumMember.GetType().GetProperty("Name");
459 return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
462 public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
464 ArgumentNotNull(enumType, "enumType");
466 var membersProperty = enumType.GetType().GetProperty("Members");
467 return membersProperty != null
468 ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
469 : Enumerable.Empty<MetadataItem>();
472 public bool EnumIsFlags(EdmType enumType)
474 ArgumentNotNull(enumType, "enumType");
476 var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
477 return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
480 public bool IsEnumType(GlobalItem edmType)
482 ArgumentNotNull(edmType, "edmType");
484 return edmType.GetType().Name == "EnumType";
487 public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
489 ArgumentNotNull(enumType, "enumType");
491 return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
494 public string CreateLiteral(object value)
496 if (value == null || value.GetType() != typeof(TimeSpan))
498 return _code.CreateLiteral(value);
501 return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
504 public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
506 ArgumentNotNull(types, "types");
507 ArgumentNotNull(sourceFile, "sourceFile");
509 var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
510 if (types.Any(item => !hash.Add(item)))
513 new CompilerError(sourceFile, -1, -1, "6023",
514 String.Format(CultureInfo.CurrentCulture, CodeGenerationTools.GetResourceString("Template_CaseInsensitiveTypeConflict"))));
520 public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
522 return GetItemsToGenerate<SimpleType>(itemCollection)
523 .Where(e => IsEnumType(e));
526 public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
528 return itemCollection
530 .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
531 .OrderBy(i => i.Name);
534 public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
536 return itemCollection
537 .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
538 .Select(g => GetGlobalItemName(g));
541 public string GetGlobalItemName(GlobalItem item)
545 return ((EdmType)item).Name;
549 return ((EntityContainer)item).Name;
553 public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
555 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
558 public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
560 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
563 public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
565 return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
568 public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
570 return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
573 public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
575 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
578 public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
580 return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
583 public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
585 return type.NavigationProperties.Where(np => np.DeclaringType == type);
588 public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
590 return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
593 public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
595 ArgumentNotNull(edmFunction, "edmFunction");
597 var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
598 return returnParamsProperty == null
599 ? edmFunction.ReturnParameter
600 : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
603 public bool IsComposable(EdmFunction edmFunction)
605 ArgumentNotNull(edmFunction, "edmFunction");
607 var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
608 return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
611 public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
613 return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
616 public TypeUsage GetReturnType(EdmFunction edmFunction)
618 var returnParam = GetReturnParameter(edmFunction);
619 return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
622 public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
624 var returnType = GetReturnType(edmFunction);
625 return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
629 public static void ArgumentNotNull<T>(T arg, string name) where T : class
633 throw new ArgumentNullException(name);