码迷,mamicode.com
首页 > 其他好文 > 详细

EF 5.0 修改T4模板(1)

时间:2017-04-15 21:38:21      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:bsp   dna   gets   null   tar   size   for   地方   灰色   

    Ps 修改EF5.0的tt文件(添加命名空间和特性and备注),之所以修改是因为,使用Json.net序列化但是又不能序列化导航属性,因为这样会使程序运行时引发一个循环引用的BUG,所以需要在tt文件中遍历的时候添加命名空间和给导航属性添加特性

 EF 5.0 tt文件修改

  1添加类备注(添加命名空间) :          

WriteHeader(codeStringGenerator, fileManager);
string summary=string.Empty;
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + ".cs");
    BeginNamespace(code);
if(entity.Documentation !=null && entity.Documentation.Summary!=null)
       summary=entity.Documentation.Summary;
     else
        summary=entity.Name;
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
using Newtonsoft.Json;
/// <summary>
/// <#=summary#>
/// </summary>

     ps: 请注意  entity这个单词 这个表示这个类的实体 entity.Name的名字就是.cs文件的名字 ,在备注的上面(灰色的地方)是引用的命名空间

 

   2 类中属性的备注

 var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null)
          {
           summary=edmProperty.Documentation.Summary;
          }
         else
          {
           summary=edmProperty.Name;
          }
#>    
    // <#=summary#>    
    <#=codeStringGenerator.Property(edmProperty)#>

 

    ps: 同样这段代码表示的是遍历这个实体的属性 可以看到edmPropetry这个单词 当然 edmPropetry.Name表示这个属性的名字

    3 导航属性添加特性

var navigationProperties = typeMapper.GetNavigationProperties(entity);
    if (navigationProperties.Any())
    {
#>

<#
        foreach (var navigationProperty in navigationProperties)
        {
#>
    [JsonIgnore]
<#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
        }
    }
#>
}
<#
    EndNamespace(code);
}

   ps: 红色位置添加特性 ,   navigationProperty顾名思义导航属性

EF 5.0 修改T4模板(1)

标签:bsp   dna   gets   null   tar   size   for   地方   灰色   

原文地址:http://www.cnblogs.com/a578024797/p/6715743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!