zoukankan      html  css  js  c++  java
  • EF架构~为EF DbContext生成的实体添加注释 【转】

    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; }
    <#+
    }
    

     保存后,它会将结果输出到与TT文件同时的cs文件中,如果模板出现错误,也会将错误信息输出到CS文件中,如果成功,就会有如何内容:

    //------------------------------------------------------------------------------
     // <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;
    

      

     

    使用EF架构时,你的实体生成方案有多种,entity object,poco,dbcontext等等,对于entity object方案生成的实体,我们感觉很臃肿,当然它的功能很强在,但有时在查看类实体时,有些麻烦,因为所有实体都在一个类文件中,有点像linq to sql,而它的类格局也与dbml有些雷同,当然这不是今天的重点,今天主要说的是当EDMX文件添加注释后,如何把注释同时添加到dbcontext实体上。

    方案:修改dbcontext的T4模版

    实现:找到以下代码块

    先为类加注释

    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>
    

      

    再为类中的属性加注释

    void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty) {}

    将原来的代码删除,替换成如下代码

  • 相关阅读:
    2018第一发:记一次【Advanced Installer】打包之旅
    Nginx 实现端口转发
    php支付宝手机网页支付类实例
    磁盘阵列操作实战
    错误修改/etc/fstab,导致系统无法开机
    linux 查看机器的cpu,操作系统等命令
    shell实现https登录
    linux tomcat配置https
    ArrayList和Vector以及synchronizedList
    java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
  • 原文地址:https://www.cnblogs.com/Ruiky/p/2749297.html
Copyright © 2011-2022 走看看