EF架构~为EF DbContext生成的实体添加注释(T4模板应用)

回到目录

相关文章系列

第八回  EF架构~将数据库注释添加导入到模型实体类中

第二十一回  EF架构~为EF DbContext生成的实体添加注释(T4模板应用)

第二十二回  EF架构~为EF DbContext生成的实体添加注释(T5模板应用

使用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)
{}

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

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文件中,如果成功,就会有如何内容:

 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //    This code was generated from a template.
 4 //
 5 //    Manual changes to this file may cause unexpected behavior in your application.
 6 //    Manual changes to this file will be overwritten if the code is regenerated.
 7 // </auto-generated>
 8 //------------------------------------------------------------------------------
 9 
10 using System;
11 using System.Collections.Generic;

而且,你的模型注释信息会被同时添加到实体上,呵呵。

 回到目录

posted @ 2012-10-31 10:24  张占岭  阅读(10538)  评论(4编辑  收藏  举报