EF架構~為(wei)EF DbContext生成(cheng)的實體添加注釋(T5模板(ban)應用)
相關文章系列
第八回 EF架構~將數(shu)據庫(ku)注釋添加導(dao)入到(dao)模型實體類中(zhong)
嗨,沒法說,EF4的TT模版加上注釋后,升級到EF5的TT模版后(hou),注(zhu)釋就不通用了(le),所以,還得再研(yan)究一下,然(ran)后(hou)把操作(zuo)方法再分享(xiang)出來,沒(mei)轍的微軟!
T4模版(ban)可(ke)(ke)能有(you)(you)些凌亂,這在T5模版(ban)里(li)(li)有(you)(you)了(le)不錯的改進,但我希望解(jie)決的問題在T5里(li)(li)并沒有(you)(you)得到解(jie)決,那就是TT類文件自動得到EDMX模型(xing)的注釋問題,可(ke)(ke)能大微(wei)的開(kai)發人員不需要實體注釋吧,嗨(hai)!
1 先加上類注釋
找(zhao)到這行(xing)代碼WriteHeader(codeStringGenerator, fileManager);
在它(ta)下面加上我們的代(dai)碼:
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)#>
同時(shi)保存TT模版文件,這時(shi)類(lei)的注釋(shi)就已經加上了
2 再加上屬性注釋
找(zhao)到這行(xing)代碼 foreach (var edmProperty in simpleProperties)
在(zai)它下面加上我們的代碼
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)#>
同時保存(cun)TT模版,這時我(wo)們的類屬性(xing)注釋就加(jia)好了
事實上,這TT模版加注釋的原理就是通過讀EDMX文件(是個XML文件)的相關注釋內容來為POCO實體類加注釋的,如果EDMX里沒有注釋,這個數據庫注釋也無法加上來,相關數據庫與EDMX注釋同步文章,可以看這篇文章EF架構~將數據庫注釋添加導入到模型實體類中。