首页 > 其他 > 详细

T4模板在项目中的使用

时间:2016-04-09 01:32:03      阅读:278      评论:0      收藏:0      [点我收藏+]

建立T4模板方法:右键添加新项->文本模板

使用T4模板生成Dal层代码如下:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#>
 
<#

CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
//修改当前tt模板相对应的edmx模型的位置
string inputFile = @"..\\Heima7.OA.Model\\DataModel.edmx";

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

#>

using Heima7.OA.Model;

namespace Heima7.OA.EFDAL
{
   
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{    
#>    public partial class <#=entity.Name#>Dal :BaseDal<<#=entity.Name#>>,IDAL.I<#=entity.Name#>Dal
    {
    } 
<#}#>

}

 技术分享

技术分享

ZJ:找规律,然后利用<#= #>把代码嵌套进去。

代码下载:http://pan.baidu.com/s/1bAP0n0

T4模板在项目中的使用

原文:http://www.cnblogs.com/shuai7boy/p/5370465.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!