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 ? ("ICollection<" + 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 ? ("ICollection<" + 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; }<#+}
After saving, it will output the result to the CS file at the same time as the TT file. If an error occurs in the template, it will also output the error information to the CS file. If the template is successful, what will happen:
//------------------------------------------------------------------------------ // <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> //------------------------------------------------------------------------------ using System; using System.Collections.Generic;
When using the EF architecture, your entity generation solutions include entity object, Poco, dbcontext, and so on. We feel bloated with the entity generated by the entity object solution, of course, it is very powerful, but sometimes it is troublesome to view class objects, because all objects are in a class file, a bit like LINQ to SQL, the class structure is similar to that of dbml. Of course, this is not the focus of today. Today we mainly talk about how to add annotations to the dbcontext object after adding annotations to the edmx file.
Solution: Modify the T4 template of dbcontext
Implementation: Find the following code block
Add comments to the class first
string summary=string.Empty;foreach (var 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; #>/// <summary>/// <#=summary#>/// </summary>
Add comments to the attributes in the class
Void writeproperty (codegenerationtools code, edmproperty ){}
Delete the original code and replace it with the following code: