123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <#@ template language="C#" debug="false" hostspecific="true"#>
- <#@ include file="EF.Utility.CS.ttinclude"#><#@
- output extension=".cs"#><#
- CodeGenerationTools code = new CodeGenerationTools(this);
- MetadataLoader loader = new MetadataLoader(this);
- CodeRegion region = new CodeRegion(this, 1);
- MetadataTools ef = new MetadataTools(this);
- string inputFile = @"EMISOnlineContext.edmx";
- EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
- string namespaceName = code.VsNamespaceSuggestion();
- EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
- WriteHeader(fileManager);
- string summary=string.Empty;
- foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
- {
- fileManager.StartNewFile(entity.Name + ".cs");
- BeginNamespace(namespaceName, code);
- if(entity.Documentation !=null && entity.Documentation.Summary!=null)
- summary=entity.Documentation.Summary;
- else
- summary=entity.Name;
- #>
- #pragma warning disable 1573
- using System;
- using System.Collections.Generic;
- /// <summary>
- /// <#=summary#>
- /// </summary>
- <#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
- {
- <#
-
- var propertiesWithDefaultValues = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity && p.DefaultValue != null);
- var collectionNavigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
- var complexProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity);
-
- if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
- {
- #>
- public <#=code.Escape(entity)#>()
- {
- <#
- foreach (var edmProperty in propertiesWithDefaultValues)
- {
- #>
- this.<#=code.Escape(edmProperty)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;
- <#
- }
- foreach (var navigationProperty in collectionNavigationProperties)
- {
- #>
- this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=code.Escape(navigationProperty.ToEndMember.GetEntityType())#>>();
- <#
- }
- foreach (var complexProperty in complexProperties)
- {
- #>
- this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();
- <#
- }
- #>
- }
- <#
- }
- var primitiveProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity);
- if (primitiveProperties.Any())
- {
- foreach (var edmProperty in primitiveProperties)
- {
- WriteProperty(code, edmProperty);
- }
- }
- if (complexProperties.Any())
- {
- #>
- <#
- foreach(var complexProperty in complexProperties)
- {
- WriteProperty(code, complexProperty);
- }
- }
- var navigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity);
- if (navigationProperties.Any())
- {
- #>
- <#
- foreach (var navigationProperty in navigationProperties)
- {
- WriteNavigationProperty(code, navigationProperty);
- }
- }
- #>
- }
- <#
- EndNamespace(namespaceName);
- }
- foreach (var complex in ItemCollection.GetItems<ComplexType>().OrderBy(e => e.Name))
- {
- fileManager.StartNewFile(complex.Name + ".cs");
- BeginNamespace(namespaceName, code);
- #>
- using System;
- <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
- {
- <#
- var complexProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex);
- var propertiesWithDefaultValues = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex && p.DefaultValue != null);
- if (propertiesWithDefaultValues.Any() || complexProperties.Any())
- {
- #>
- public <#=code.Escape(complex)#>()
- {
- <#
- foreach (var edmProperty in propertiesWithDefaultValues)
- {
- #>
- this.<#=code.Escape(edmProperty)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;
- <#
- }
- foreach (var complexProperty in complexProperties)
- {
- #>
- this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();
- <#
- }
- #>
- }
- <#
- }
- var primitiveProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex);
- if (primitiveProperties.Any())
- {
- foreach(var edmProperty in primitiveProperties)
- {
- WriteProperty(code, edmProperty);
- }
- }
- if (complexProperties.Any())
- {
- #>
- <#
- foreach(var edmProperty in complexProperties)
- {
- WriteProperty(code, edmProperty);
- }
- }
- #>
- }
- <#
- EndNamespace(namespaceName);
- }
- if (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))
- {
- return "";
- }
- fileManager.Process();
- #>
- <#+
- void WriteHeader(EntityFrameworkTemplateFileManager fileManager)
- {
- fileManager.StartHeader();
- #>
- //------------------------------------------------------------------------------
- // <auto-generated>
- // This code was generated from a template.
- //
- // Manual changes to this file may cause unexpected behavior in your application.
- // Manual changes to this file will be overwritten if the code is regenerated.
- // </auto-generated>
- //------------------------------------------------------------------------------
- <#+
- fileManager.EndBlock();
- }
- void BeginNamespace(string namespaceName, CodeGenerationTools code)
- {
- CodeRegion region = new CodeRegion(this);
- if (!String.IsNullOrEmpty(namespaceName))
- {
- #>
- namespace <#=code.EscapeNamespace(namespaceName)#>
- {
- <#+
- PushIndent(CodeRegion.GetIndent(1));
- }
- }
- void EndNamespace(string namespaceName)
- {
- if (!String.IsNullOrEmpty(namespaceName))
- {
- PopIndent();
- #>
- }
- <#+
- }
- }
- void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
- {
- if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null)
- {
- WriteProperty(Accessibility.ForProperty(edmProperty),
- code.Escape(edmProperty.Documentation.Summary),
- code.Escape(edmProperty.TypeUsage),
- code.Escape(edmProperty),
- code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
- code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
- }
- else
- {
- WriteProperty(Accessibility.ForProperty(edmProperty),
- code.Escape(edmProperty.Name),
- code.Escape(edmProperty.TypeUsage),
- code.Escape(edmProperty),
- code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
- code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
- }
- }
- void WriteNavigationProperty(CodeGenerationTools code, NavigationProperty navigationProperty)
- {
- var endType = code.Escape(navigationProperty.ToEndMember.GetEntityType());
- if (navigationProperty.Documentation != null && navigationProperty.Documentation.Summary != null)
- {
- WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)),
- code.Escape(navigationProperty.Documentation.Summary),
- navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("HashSet<" + endType + ">") : endType,
- code.Escape(navigationProperty),
- code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
- code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
- }
- else
- {
- WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)),
- code.Escape(navigationProperty.Name),
- navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("HashSet<" + endType + ">") : endType,
- code.Escape(navigationProperty),
- code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
- code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
- }
- }
- void WriteProperty(string accessibility, string summary, string type, string name, string getterAccessibility, string setterAccessibility)
- {
- #>
- /// <summary>
- /// <#=summary#>
- /// </summary>
- <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; }
- <#+
- }
- string PropertyVirtualModifier(string accessibility)
- {
- return accessibility + (accessibility != "private" ? " virtual" : "");
- }
- bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
- {
- var alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
- foreach(var type in itemCollection.GetItems<StructuralType>())
- {
- if (!(type is EntityType || type is ComplexType))
- {
- continue;
- }
- if (alreadySeen.ContainsKey(type.FullName))
- {
- Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
- return false;
- }
- else
- {
- alreadySeen.Add(type.FullName, true);
- }
- }
- return true;
- }
- #>
|