博客
关于我
Ef+T4模板实现代码快速生成器
阅读量:413 次
发布时间:2019-03-06

本文共 3704 字,大约阅读时间需要 12 分钟。

转载请注明地址: 

效果如图,(点击demo可下载案例)

 

项目结构如图

T4BLL添加BLL.tt文件;

T4Model添加Model文件;

T4DAL添加DAL.tt文件;

T4DAL 添加ADO.NET Entity Data Model选择dababase first 模式;


 

打开Model1.edmx文件下的Model1.tt打开,复制内容替换了T4Model项目下Model.tt文件;

打开Model1.edmx文件下的Model.tt从<#=codeStringGenerator.EntityClassOpening(entity)#>{ 开始选择至 }<# EndNamespace(code); 删除

如下图

复制Model1.tt内容替换BLL.tt、DAL.tt内容,现在Model1.tt已经没有什么意义了,可以删除。


 

打开T4Model的T4模板,修改如下代码,修改完成后保存下就可以生成出Edmx中添加类的Model。

const string inputFile = @"Model1.edmx";//更改为string inputFile = Host.ResolveAssemblyReference("$(ProjectDir)").Replace("T4Model","T4DAL")+"/Model1.edmx";

 

打开T4DAL的T4模板

fileManager.StartNewFile(entity.Name + ".cs");//更改为fileManager.StartNewFile(entity.Name + "_DAL.cs");//---分割线--- public string EntityClassOpening(EntityType entity)    {        return string.Format(            CultureInfo.InvariantCulture,            "{0} {1}partial class {2}{3}",            Accessibility.ForType(entity),            _code.SpaceAfter(_code.AbstractOption(entity)),            _code.Escape(entity),            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));    }//更改为 public string EntityClassOpening(EntityType entity)    {        return string.Format(            CultureInfo.InvariantCulture,            "{0} {1}partial class {2}_DAL{3}",            Accessibility.ForType(entity),            _code.SpaceAfter(_code.AbstractOption(entity)),            _code.Escape(entity),            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));    }//---分割线---更改为foreach (var entity in typeMapper.GetItemsToGenerate
(itemCollection)){ fileManager.StartNewFile(entity.Name + "_DAL.cs"); BeginNamespace(code);#>using T4Model;<#=codeStringGenerator.UsingDirectives(inHeader: false)#><#=codeStringGenerator.EntityClassOpening(entity)#>{ ///
/// 新增 /// ///
///
public <#= entity.Name #> Add(<#= entity.Name #> entity){ //内容实现 return null; }}<# EndNamespace(code);}

 


 

打开T4BLL的T4模板

const string inputFile = @"Model1.edmx";//更改为string inputFile = Host.ResolveAssemblyReference("$(ProjectDir)").Replace("T4BLL","T4DAL")+"/Model1.edmx"; //---分割线---fileManager.StartNewFile(entity.Name + ".cs");//更改为fileManager.StartNewFile(entity.Name + "_BLL.cs");//---分割线--- public string EntityClassOpening(EntityType entity)    {        return string.Format(            CultureInfo.InvariantCulture,            "{0} {1}partial class {2}{3}",            Accessibility.ForType(entity),            _code.SpaceAfter(_code.AbstractOption(entity)),            _code.Escape(entity),            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));    }//更改为 public string EntityClassOpening(EntityType entity)    {        return string.Format(            CultureInfo.InvariantCulture,            "{0} {1}partial class {2}_BLL{3}",            Accessibility.ForType(entity),            _code.SpaceAfter(_code.AbstractOption(entity)),            _code.Escape(entity),            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));    }//---分割线---更为foreach (var entity in typeMapper.GetItemsToGenerate
(itemCollection)){ fileManager.StartNewFile(entity.Name + "_BLL.cs"); BeginNamespace(code);#>using T4DAL;using T4Model;<#=codeStringGenerator.UsingDirectives(inHeader: false)#><#=codeStringGenerator.EntityClassOpening(entity)#>{ <#= entity.Name #>_DAL dal = new <#= entity.Name #>_DAL(); ///
/// 新增 /// ///
///
public <#= entity.Name #> Add(<#= entity.Name #> entity){ return dal.Add(entity); }}<# EndNamespace(code);}

Host.ResolveAssemblyReference("$(ProjectDir)")是获取项目路径的方法,通过替换项目名称来寻找到edmx文件,从而实现T4模板分离在不同的类库中。

 

 

你可能感兴趣的文章
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>