<#@ 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 = @"ModelTrackingV2.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); WriteHeader(fileManager); foreach (var entity in ItemCollection.GetItems().OrderBy(e => e.Name)) { fileManager.StartNewFile(entity.Name + ".cs"); BeginNamespace(namespaceName, code); #> using System; using System.Collections.Generic; <#=SummaryComment(entity)#> <#=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) { WriteSummaryComment(edmProperty); WriteProperty(code, edmProperty); } } if (complexProperties.Any()) { #> <# foreach(var complexProperty in complexProperties) { WriteSummaryComment(complexProperty); WriteProperty(code, complexProperty); } } var navigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity); if (navigationProperties.Any()) { #> <# foreach (var navigationProperty in navigationProperties) { WriteSummaryComment(navigationProperty); WriteNavigationProperty(code, navigationProperty); } } #> } <# EndNamespace(namespaceName); } foreach (var complex in ItemCollection.GetItems().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(); #> <#+ string GetResourceString(string resourceName) { if(_resourceManager == null) { _resourceManager = new System.Resources.ResourceManager("System.Data.Entity.Design", typeof(System.Data.Entity.Design.MetadataItemCollectionFactory).Assembly); } return _resourceManager.GetString(resourceName, null); } System.Resources.ResourceManager _resourceManager; void WriteHeader(EntityFrameworkTemplateFileManager fileManager) { fileManager.StartHeader(); #> //------------------------------------------------------------------------------ // // <#=GetResourceString("Template_GeneratedCodeCommentLine1")#> // // <#=GetResourceString("Template_GeneratedCodeCommentLine2")#> // <#=GetResourceString("Template_GeneratedCodeCommentLine3")#> // //------------------------------------------------------------------------------ <#+ fileManager.EndBlock(); } void BeginNamespace(string namespaceName, CodeGenerationTools code) { CodeRegion region = new CodeRegion(this); if (!String.IsNullOrEmpty(namespaceName)) { #> namespace <#=code.EscapeNamespace(namespaceName)#> { <#+ PushIndent("\t"); } } void EndNamespace(string namespaceName) { if (!String.IsNullOrEmpty(namespaceName)) { PopIndent(); #> } <#+ } } void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty) { WriteProperty(Accessibility.ForProperty(edmProperty), 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()); WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)), navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, code.Escape(navigationProperty), code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); } void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility) { #> <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; } <#+ } string PropertyVirtualModifier(string accessibility) { return accessibility + (accessibility != "private" ? " virtual" : ""); } bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection) { var alreadySeen = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach(var type in itemCollection.GetItems()) { 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; } const string XMLCOMMENT_START = "/// "; string SummaryComment(MetadataItem item) { if (item.Documentation != null && item.Documentation.Summary != null) { string formattedComment = XMLCOMMENT_START + "" + Environment.NewLine; formattedComment += XMLCOMMENT_START + PrefixLinesOfMultilineComment(XMLCOMMENT_START, XmlEntityize(item.Documentation.Summary)); formattedComment += Environment.NewLine + XMLCOMMENT_START + ""; return formattedComment; } return string.Empty; } void WriteSummaryComment(MetadataItem item) { foreach(string line in SummaryComment(item).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { #> <#=line#> <#+ } } string PrefixLinesOfMultilineComment(string prefix, string comment) { return comment.Replace(Environment.NewLine, Environment.NewLine + prefix); } string XmlEntityize(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } text = text.Replace("<","<").Replace(">",">"); text = text.Replace("\r", Environment.NewLine).Replace("\n",Environment.NewLine); return text; } #>