使用
- 初始化准备
获取MultipleOutputHelper.ttinclude文件代码
描述:找不到的同学可以通过Github.com直通车托管上获取(https://github.com/damieng/DamienGKit/blob/master/T4/MultipleOutputHelper/MultipleOutputHelper.ttinclude)
在T4模板中使用include指令导入MultipleOutputHelper.ttinclude文件或者将MultipleOutputHelper.ttinclude的代码复制在T4模板内。
- 加载代码如下
<#@include file="$(ProjectDir)\MultipleOutputHelper.ttinclude"#>
场景应用
- 自定义T4模板文件,文件名为“Template.tt”,具体代码如下:
1 <#@ template language="C#" hostspecific="True" debug="false"#> 2 <#@include file="$(ProjectDir)\DBHelper\DbEntity.ttinclude"#> 3 <#@ assembly name="System" #> 4 <#@ assembly name="System.Core" #> 5 <#@ assembly name="System.Data" #> 6 <#@ assembly name="System.Data.DataSetExtensions" #> 7 <#@ import namespace="System" #> 8 <#@ import namespace="System.Data" #> 9 <#@ import namespace="System.Data.SqlClient" #> 10 <# var manager = Manager.Create(Host, GenerationEnvironment); #> 11 <# 12 //你的当前model的命名空间 13 string ModelNameSpace="Snail.Entity"; 14 string copyright="一介布衣乄"; 15 string version="V1.0"; 16 string author="Allon"; 17 18 //你的数据库连接字符串 19 string ConnectStr = "Data Source"; 20 SqlConnection MySqlConnection = new SqlConnection(ConnectStr); 21 //此sql语句找到你指定数据库下的所有表 22 string SelectYourTableNameStr = "SELECT * FROM sys.sysobjects WHERE TYPE='U'"; 23 SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter(SelectYourTableNameStr, MySqlConnection); 24 DataSet MyDataSet = new DataSet(); 25 MySqlDataAdapter.Fill(MyDataSet, "cacheTable"); 26 //得到table表 27 DataTable MyDataTable = MyDataSet.Tables["cacheTable"]; 28 int MyDataTableRowsCount = MyDataTable.Rows.Count; 29 for (int i = 0; i < MyDataTableRowsCount; i++) 30 { 31 //表名 32 //MyDataTable.Rows[i]["name"].ToString(); 33 //此sql语句找到你当前表下的所有键值,属性,是否为null,长度。 34 string SelectYourKeyStr = "SELECT [col].[COLUMN_NAME] AS keyname,"+ 35 " [col].[DATA_TYPE] AS keyproperty,"+ 36 " [col].[CHARACTER_OCTET_LENGTH] AS [length],"+ 37 " CASE [col].[IS_NULLABLE]"+ 38 " WHEN 'YES' THEN"+ 39 " 1"+ 40 " ELSE"+ 41 " 0"+ 42 " END AS isnullable,"+ 43 " CASE"+ 44 " WHEN [ccu].[CONSTRAINT_NAME] IS NOT NULL THEN"+ 45 " 1"+ 46 " ELSE"+ 47 " 0"+ 48 " END AS IsPrimaryKey,"+ 49 " COLUMNPROPERTY(OBJECT_ID('"+ MyDataTable.Rows[i]["name"].ToString() + "'), [col].[COLUMN_NAME], 'IsIdentity') AS IsIdentity"+ 50 " FROM [INFORMATION_SCHEMA].[COLUMNS] col"+ 51 " LEFT JOIN [INFORMATION_SCHEMA].[CONSTRAINT_COLUMN_USAGE] ccu"+ 52 " ON [ccu].[TABLE_NAME] = [col].[TABLE_NAME]"+ 53 " AND [ccu].[COLUMN_NAME] = [col].[COLUMN_NAME]"+ 54 " AND [ccu].[CONSTRAINT_NAME] LIKE 'PK_%'"+ 55 " WHERE [col].[TABLE_NAME] = '"+ MyDataTable.Rows[i]["name"].ToString() + "'"; 56 57 MySqlDataAdapter = new SqlDataAdapter(SelectYourKeyStr, MySqlConnection); 58 MyDataSet = new DataSet(); 59 MySqlDataAdapter.Fill(MyDataSet, "cacheTable"); 60 //得到key表 61 DataTable MyKeyTable = MyDataSet.Tables["cacheTable"]; 62 //tt模板输出文件 63 manager.StartNewFile(MyDataTable.Rows[i]["name"].ToString() +".cs"); 64 #> 65 //----------------------------------------------------------------------- 66 // * Copyright (C) <#= copyright #> 67 // * Version : <#= version #> 68 // * Author : <#= author #> 69 // * FileName: <#= MyDataTable.Rows[i]["name"] #>.cs 70 // * History : Created by T4 <#= DateTime.Now #> 71 // </copyright> 72 //----------------------------------------------------------------------- 73 using System; 74 namespace <#=ModelNameSpace#> 75 { 76 public class <#=MyDataTable.Rows[i]["name"].ToString()#> 77 { 78 <# for (int keyi = 0; keyi < MyKeyTable.Rows.Count; keyi++) 79 { 80 string keypropertyOutput = string.Empty; 81 string isKey=string.Empty; 82 if(MyKeyTable.Rows[keyi]["IsPrimaryKey"].ToString()=="1" && MyKeyTable.Rows[keyi]["IsIdentity"].ToString()=="1"){ 83 isKey=", IsPrimaryKey = true, IsIdentity = true"; 84 }else if(MyKeyTable.Rows[keyi]["IsPrimaryKey"].ToString()=="1"){ 85 isKey=", IsPrimaryKey = true"; 86 } 87 switch (MyKeyTable.Rows[keyi]["keyproperty"].ToString().ToLower()) 88 { 89 case "smallint": 90 keypropertyOutput = "short"; 91 break; 92 case "int": 93 keypropertyOutput = "int"; 94 break; 95 case "bigint": 96 keypropertyOutput = "long"; 97 break; 98 case "real": 99 keypropertyOutput = "float"; 100 break; 101 case "float": 102 keypropertyOutput = "double"; 103 break; 104 case "money": 105 keypropertyOutput = "decimal"; 106 break; 107 case "datetime": 108 keypropertyOutput = "DateTime"; 109 break; 110 case "uniqueidentifier": 111 keypropertyOutput = "Guid"; 112 break; 113 114 case "bit": 115 keypropertyOutput = "bool"; 116 break; 117 case "tinyint": 118 keypropertyOutput = "byte"; 119 break; 120 case "image": 121 keypropertyOutput = "byte[]"; 122 break; 123 case "binary": 124 keypropertyOutput = "byte[]"; 125 break; 126 default: 127 keypropertyOutput = "string"; 128 break; 129 } 130 //可为null且不是string(就是值类型)可以加? 131 if (MyKeyTable.Rows[keyi]["isnullable"].ToString()=="1"&& keypropertyOutput!="string") 132 { 133 if (keypropertyOutput == "byte[]") 134 { 135 keypropertyOutput = "byte?[]"; 136 } 137 else 138 { 139 keypropertyOutput = keypropertyOutput + "?"; 140 } 141 } 142 #> 143 144 public <#=keypropertyOutput#> <#=MyKeyTable.Rows[keyi]["keyname"].ToString()#> { get; set; } 145 <# 146 } 147 #> 148 } 149 } 150 <# //代码结束 151 manager.EndBlock(); 152 MySqlConnection.Close(); 153 } 154 #> 155 <# manager.Process(true); #>
- 输出文件预览: