EF architecture ~ Add comments to the entity generated by EF dbcontext

Source: Internet
Author: User
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:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.