经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
真实项目中VS2015中自建T4模板生成文件的使用
来源:cnblogs  作者:王银行  时间:2018/11/14 10:04:58  对本文有异议

有可能许多小伙伴们发现,vs2015和2012的自带T4模板中的.tt文件改变非常之多,如果仅仅copyEF系统自己生成的模板文件,那可累了。以下是我自己整理的在2012和2015中都可以试用的代码。

  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF.Utility.CS.ttinclude"#><#@
  3. output extension=".cs"#>
  4. <#
  5. CodeGenerationTools code = new CodeGenerationTools(this);
  6. MetadataLoader loader = new MetadataLoader(this);
  7. CodeRegion region = new CodeRegion(this, 1);
  8. MetadataTools ef = new MetadataTools(this);
  9. //修改当前tt模板相对应的edmx模型的位置
  10. string inputFile = @"..\\Model\\DataModel.edmx";
  11. EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
  12. string namespaceName = code.VsNamespaceSuggestion();
  13. EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
  14. #>
  15.  
  16. using Model;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. namespace IBLL
  23. {
  24. <#
  25. foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  26. {
  27. #>
  28. public partial interface I<#=entity.Name#>Service:IBaseService<<#=entity.Name#>>
  29. {
  30. }
  31. <#}#>
  32. }

注意:string inputFile = @"..\\Model\\DataModel.edmx";

这个是需要插入与当前项目对应的EF模型类的位置

  1. foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
    这句很明显是取EF类中文件的语句

    生成效果如下:注意原类需要添加partial标签

 



 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号