
Ef+T4模板实现代码快速生成器
发布日期:2021-05-09 01:03:34
浏览次数:15
分类:博客文章
本文共 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模板分离在不同的类库中。
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月30日 21时28分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
面试题 08.01. 三步问题
2019-03-15
剑指 Offer 11. 旋转数组的最小数字
2019-03-15
word文档注入(追踪word文档)未完
2019-03-15
作为我的第一篇csdn博客吧
2019-03-15
ajax异步提交失败
2019-03-15
一道简单的访问越界、栈溢出pwn解题记录
2019-03-15
ubuntu18.04.4版本安装docker教程
2019-03-15
Stream 某些API
2019-03-15
关于项目中 对Java 的为空判断整理
2019-03-15
测试调用另一台电脑ip是否有用
2019-03-15
mos-excel集成文档
2019-03-15
chat 快问!
2019-03-15
3.jdk的环境配置
2019-03-15
2.连接池
2019-03-15
2.Html与CSS
2019-03-15
3&4.javascript
2019-03-15
6.Xml
2019-03-15
7.tomcat_servlet
2019-03-15
3.Ajax&Json
2019-03-15