参考自这位大狮的: https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt
项目Demo下载 http://download.csdn.net/detail/qq_21533697/9904071
- 支持Oracle,MSSQL,SQLite
- Demo项目是个抽奖小程序,抽奖只用到了LuckDraw表
- Demo用的SQLite包含库方便直接运行
- 里面用到Dapper就只写了Model层的模板,
文件目录
T4库
表结构读取抽象类 SchemaReader.ttinclude
1 <#+ 2 /* 3 The contents of this file are subject to the New BSD 4 License (the "License"); you may not use this file 5 except in compliance with the License. You may obtain a copy of 6 the License at http://www.opensource.org/licenses/bsd-license.php 7 8 Software distributed under the License is distributed on an 9 "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 10 implied. See the License for the specific language governing 11 rights and limitations under the License. 12 */ 13 14 string ConnectionString = ""; 15 string TableFilter = ""; 16 string TopNamespace = ""; 17 string Namespace = ""; 18 string ClassPrefix = ""; 19 string ClassSuffix = ""; 20 string SchemaName = null; 21 bool IncludeViews; 22 string[] ExcludeTablePrefixes = new string[]{}; 23 string _connectionString=""; 24 string _providerName=""; 25 26 static Regex rxCleanUp = new Regex(@"[^\w\d_]", RegexOptions.Compiled); 27 28 static Func<string, string> CleanUp = (str) => 29 { 30 str = rxCleanUp.Replace(str, "_"); 31 if (char.IsDigit(str[0])) str = "_" + str; 32 33 return str; 34 }; 35 36 string CheckNullable(Column col) 37 { 38 string result=""; 39 if(col.IsNullable && 40 col.PropertyType !="byte[]" && 41 col.PropertyType !="string" && 42 col.PropertyType !="Microsoft.SqlServer.Types.SqlGeography" && 43 col.PropertyType !="Microsoft.SqlServer.Types.SqlGeometry" 44 ) 45 result="?"; 46 return result; 47 } 48 49 static bool IsExcluded(string tablename, string[] ExcludeTablePrefixes) 50 { 51 for (int i = 0; i < ExcludeTablePrefixes.Length; i++) 52 { 53 string s = ExcludeTablePrefixes[i]; 54 if(tablename.StartsWith(s)) return true; 55 } 56 return false; 57 } 58 59 60 abstract class SchemaReader 61 { 62 public abstract Tables ReadSchema(string connstr, string tableFilter); 63 public GeneratedTextTransformation outer; 64 public void WriteLine(string o) 65 { 66 outer.WriteLine(o); 67 } 68 69 public string GetPropertyType(string sqlType) 70 { 71 string sysType = "string"; 72 switch (sqlType) 73 { 74 case "bigint": 75 sysType = "long"; 76 break; 77 case "smallint": 78 sysType = "short"; 79 break; 80 case "int": 81 case "number": 82 case "integer": 83 sysType = "int"; 84 break; 85 case "uniqueidentifier": 86 sysType = "Guid"; 87 break; 88 case "smalldatetime": 89 case "datetime": 90 case "date": 91 case "time": 92 sysType = "DateTime"; 93 break; 94 case "float": 95 sysType = "double"; 96 break; 97 case "real": 98 sysType = "float"; 99 break; 100 case "numeric": 101 case "smallmoney": 102 case "decimal": 103 case "money": 104 sysType = "decimal"; 105 break; 106 case "tinyint": 107 sysType = "byte"; 108 break; 109 case "bit": 110 sysType = "bool"; 111 break; 112 case "image": 113 case "binary": 114 case "varbinary": 115 case "timestamp": 116 sysType = "byte[]"; 117 break; 118 case "geography": 119 sysType = "Microsoft.SqlServer.Types.SqlGeography"; 120 break; 121 case "geometry": 122 sysType = "Microsoft.SqlServer.Types.SqlGeometry"; 123 break; 124 } 125 return sysType; 126 } 127 } 128 129 public class Table 130 { 131 public List<Column> Columns; 132 public string Name; 133 public string Schema; 134 public bool IsView; 135 public string CleanName; 136 public string ClassName; 137 public string SequenceName; 138 public bool Ignore; 139 140 public Column PK 141 { 142 get 143 { 144 return this.Columns.SingleOrDefault(x=>x.IsPK); 145 } 146 } 147 148 public Column GetColumn(string columnName) 149 { 150 return Columns.Single(x=>string.Compare(x.Name, columnName, true)==0); 151 } 152 153 public Column this[string columnName] 154 { 155 get 156 { 157 return GetColumn(columnName); 158 } 159 } 160 161 } 162 163 public class Column 164 { 165 public string Name; 166 public string PropertyName; 167 public string PropertyType; 168 public string DbType; 169 public bool IsPK; 170 public bool IsNullable; 171 public bool IsAutoIncrement; 172 public bool Ignore; 173 } 174 175 public class Tables : List<Table> 176 { 177 public Tables() 178 { 179 } 180 181 public Table GetTable(string tableName) 182 { 183 return this.Single(x=>string.Compare(x.Name, tableName, true)==0); 184 } 185 186 public Table this[string tableName] 187 { 188 get 189 { 190 return GetTable(tableName); 191 } 192 } 193 194 } 195 196 #>