前言
此文是文章使用T4模板根据数据库生成model层的延续。
上篇文章讲使用T4模板生成Model层的cs文件。现在想来,既然Model层都可以生成,那也可以生成其他层吧。
解决方案内不同层的项目后缀名一般相同或者符合某种规则,如DAL层的UserInfoDAL或者接口层IDAL层的是IUserInfoDAL,如此一来我们可以设置代码来生成相应的文件名。
步骤
代码
步骤基本与上篇文章相同,只是t4模板里面的代码换成了如下代码。
<#@ template language="C#" hostspecific="True" debug="false"#>
<#@include file="Manager.ttinclude"#>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Data.DataSetExtensions" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>
<#
if(true)
{
//请添加你的项目名称后缀名与对应的命名空间
Dictionary<string, string> ProjectDictionary = new Dictionary<string, string>();
ProjectDictionary.Add("BLL", "MyTest.BLL");
ProjectDictionary.Add("DAL", "MyTest.DAL");
//此如果是model层请使用键值Model
//ProjectDictionary.Add("Model", "MyTest.Model");
//因为UI层apsx页面特殊,暂时不支持。
//我想是支持cshtml文件的,但是懒得写了。
//ProjectDictionary.Add("UI", "MyTest.UI");
List<string> ProjectDictionaryKeyToLower = new List<string>();
foreach (string key in ProjectDictionary.Keys)
{
ProjectDictionaryKeyToLower.Add(key.ToLower());
}
//你的数据库连接字符串
string ConnectStr = "Data Source=.;database=RoadFlowWebForm3;integrated Security =true;";
SqlConnection MySqlConnection =