相關文章系列
嗨,沒法說,EF4的TT模版加上注釋后,升級到EF5的TT模版后,注釋就不通用了,所以,還得再研究一下,然后把操作方法再分享出來,沒轍的微軟!
T4模版可能有些凌亂,這在T5模版里有了不錯的改進,但我希望解決的問題在T5里並沒有得到解決,那就是TT類文件自動得到EDMX模型的注釋問題,可能大微的開發人員不需要實體注釋吧,嗨!
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)#> /// <summary> /// <#=summary#> /// </summary> <#=codeStringGenerator.EntityClassOpening(entity)#>
同時保存TT模版文件,這時類的注釋就已經加上了

2 再加上屬性注釋
找到這行代碼 foreach (var edmProperty in simpleProperties)
在它下面加上我們的代碼
foreach (var edmProperty in simpleProperties) { if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null) { summary=edmProperty.Documentation.Summary; } else { summary=""; }
#>
/// <summary>
/// <#=summary#>
/// </summary>
<#=codeStringGenerator.Property(edmProperty)#>
同時保存TT模版,這時我們的類屬性注釋就加好了
事實上,這TT模版加注釋的原理就是通過讀EDMX文件(是個XML文件)的相關注釋內容來為POCO實體類加注釋的,如果EDMX里沒有注釋,這個數據庫注釋也無法加上來,相關數據庫與EDMX注釋同步文章,可以看這篇文章EF架構~將數據庫注釋添加導入到模型實體類中。
