在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇。T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗)。而这其中我们使用的最普遍的是根据数据库生成实体类。 工欲善其事必先利其器,在这之前先来介绍一
在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇。T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗)。而这其中我们使用的最普遍的是根据数据库生成实体类。
工欲善其事必先利其器,在这之前先来介绍一款T4编辑器T4 Editor,我们可以点击链接去下载然后安装,不过还是推荐大家直接在VS扩展管理器里直接安装来的方便 工具->扩展管理器->联机库 搜索 "T4 Editor",选择第一项 "tangible T4 Editor 2.0 plus modeling tools for VS2010" 进行安装即可,如下图所示:
安装上T4 Editor后,编辑T4模板是就有代码着色和智能提示了,下图为安装T4 Editor后的代码着色效果,怎么样是不是耳目一新,呵呵
接下来开始正式进入我们的主题,从数据库自动生成实体类
<span><span>#@ output </span><span>extension</span><span>=".cs"</span><span> #</span><span>></span></span>
<span><span>#@ assembly </span><span>name</span><span>="System.Core.dll"</span><span> #</span><span>></span> <span><span>#@ assembly </span><span>name</span><span>="System.Data.dll"</span><span> #</span><span>></span> <span><span>#@ assembly </span><span>name</span><span>="System.Data.DataSetExtensions.dll"</span><span> #</span><span>></span> <span><span>#@ assembly </span><span>name</span><span>="System.Xml.dll"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.Xml"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.Linq"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.Data"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.Data.SqlClient"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.Collections.Generic"</span><span> #</span><span>></span> <span><span>#@ import </span><span>namespace</span><span>="System.IO"</span><span> #</span><span>></span></span></span></span></span></span></span></span></span></span></span></span>
DbHelper.ttinclude
public <span>class</span><span> DbHelper { </span><span>#region</span> GetDbTables <span>public</span> <span>static</span> List<dbtable> GetDbTables(<span>string</span> connectionString, <span>string</span> database, <span>string</span> tables = <span>null</span><span>) { </span><span>if</span> (!<span>string</span><span>.IsNullOrEmpty(tables)) { tables </span>= <span>string</span>.Format(<span>"</span><span> and obj.name in ('{0}')</span><span>"</span>, tables.Replace(<span>"</span><span>,</span><span>"</span>, <span>"</span><span>','</span><span>"</span><span>)); } </span><span>#region</span> SQL <span>string</span> sql = <span>string</span>.Format(<span>@"</span><span>SELECT obj.name tablename, schem.name schemname, idx.rows, CAST ( CASE WHEN (SELECT COUNT(1) FROM sys.indexes WHERE object_id= obj.OBJECT_ID AND is_primary_key=1) >=1 THEN 1 ELSE 0 END AS BIT) HasPrimaryKey from {0}.sys.objects obj inner join {0}.dbo.sysindexes idx on obj.object_id=idx.id and idx.indid<span>"</span><span>, database, tables); </span><span>#endregion</span><span> DataTable dt </span>=<span> GetDataTable(connectionString, sql); </span><span>return</span> dt.Rows.Cast<datarow>().Select(row => <span>new</span><span> DbTable { TableName </span>= row.Fieldstring</datarow></span>>(<span>"</span><span>tablename</span><span>"</span><span>), SchemaName </span>= row.Fieldstring>(<span>"</span><span>schemname</span><span>"</span><span>), Rows </span>= row.Fieldint>(<span>"</span><span>rows</span><span>"</span><span>), HasPrimaryKey </span>= row.Fieldbool>(<span>"</span><span>HasPrimaryKey</span><span>"</span><span>) }).ToList(); } </span><span>#endregion</span> <span>#region</span> GetDbColumns <span>public</span> <span>static</span> List<dbcolumn> GetDbColumns(<span>string</span> connectionString, <span>string</span> database, <span>string</span> tableName, <span>string</span> schema = <span>"</span><span>dbo</span><span>"</span><span>) { </span><span>#region</span> SQL <span>string</span> sql = <span>string</span>.Format(<span>@"</span><span> WITH indexCTE AS ( SELECT ic.column_id, ic.index_column_id, ic.object_id FROM {0}.sys.indexes idx INNER JOIN {0}.sys.index_columns ic ON idx.index_id = ic.index_id AND idx.object_id = ic.object_id WHERE idx.object_id =OBJECT_ID(@tableName) AND idx.is_primary_key=1 ) select colm.column_id ColumnID, CAST(CASE WHEN indexCTE.column_id IS NULL THEN 0 ELSE 1 END AS BIT) IsPrimaryKey, colm.name ColumnName, systype.name ColumnType, colm.is_identity IsIdentity, colm.is_nullable IsNullable, cast(colm.max_length as int) ByteLength, ( case when systype.name='nvarchar' and colm.max_length>0 then colm.max_length/2 when systype.name='nchar' and colm.max_length>0 then colm.max_length/2 when systype.name='ntext' and colm.max_length>0 then colm.max_length/2 else colm.max_length end ) CharLength, cast(colm.precision as int) Precision, cast(colm.scale as int) Scale, prop.value Remark from {0}.sys.columns colm inner join {0}.sys.types systype on colm.system_type_id=systype.system_type_id and colm.user_type_id=systype.user_type_id left join {0}.sys.extended_properties prop on colm.object_id=prop.major_id and colm.column_id=prop.minor_id LEFT JOIN indexCTE ON colm.column_id=indexCTE.column_id AND colm.object_id=indexCTE.object_id where colm.object_id=OBJECT_ID(@tableName) order by colm.column_id</span><span>"</span><span>, database); </span><span>#endregion</span><span> SqlParameter param </span>= <span>new</span> SqlParameter(<span>"</span><span>@tableName</span><span>"</span>, SqlDbType.NVarChar, <span>100</span>) { Value = <span>string</span>.Format(<span>"</span><span>{0}.{1}.{2}</span><span>"</span><span>, database, schema, tableName) }; DataTable dt </span>=<span> GetDataTable(connectionString, sql, param); </span><span>return</span> dt.Rows.Cast<datarow>().Select(row => <span>new</span><span> DbColumn() { ColumnID </span>= row.Fieldint>(<span>"</span><span>ColumnID</span><span>"</span><span>), IsPrimaryKey </span>= row.Fieldbool>(<span>"</span><span>IsPrimaryKey</span><span>"</span><span>), ColumnName </span>= row.Fieldstring>(<span>"</span><span>ColumnName</span><span>"</span><span>), ColumnType </span>= row.Fieldstring>(<span>"</span><span>ColumnType</span><span>"</span><span>), IsIdentity </span>= row.Fieldbool>(<span>"</span><span>IsIdentity</span><span>"</span><span>), IsNullable </span>= row.Fieldbool>(<span>"</span><span>IsNullable</span><span>"</span><span>), ByteLength </span>= row.Fieldint>(<span>"</span><span>ByteLength</span><span>"</span><span>), CharLength </span>= row.Fieldint>(<span>"</span><span>CharLength</span><span>"</span><span>), Scale </span>= row.Fieldint>(<span>"</span><span>Scale</span><span>"</span><span>), Remark </span>= row[<span>"</span><span>Remark</span><span>"</span><span>].ToString() }).ToList(); } </span><span>#endregion</span> <span>#region</span> GetDataTable <span>public</span> <span>static</span> DataTable GetDataTable(<span>string</span> connectionString, <span>string</span> commandText, <span>params</span><span> SqlParameter[] parms) { </span><span>using</span> (SqlConnection connection = <span>new</span><span> SqlConnection(connectionString)) { SqlCommand command </span>=<span> connection.CreateCommand(); command.CommandText </span>=<span> commandText; command.Parameters.AddRange(parms); SqlDataAdapter adapter </span>= <span>new</span><span> SqlDataAdapter(command); DataTable dt </span>= <span>new</span><span> DataTable(); adapter.Fill(dt); </span><span>return</span><span> dt; } } </span><span>#endregion</span><span> } </span><span>#region</span> DbTable <span>///</span> <span><summary></summary></span> <span>///</span><span> 表结构 </span><span>///</span> <span></span> <span>public</span> <span>sealed</span> <span>class</span><span> DbTable { </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 表名称 </span><span>///</span> <span></span> <span>public</span> <span>string</span> TableName { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 表的架构 </span><span>///</span> <span></span> <span>public</span> <span>string</span> SchemaName { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 表的记录数 </span><span>///</span> <span></span> <span>public</span> <span>int</span> Rows { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 是否含有主键 </span><span>///</span> <span></span> <span>public</span> <span>bool</span> HasPrimaryKey { <span>get</span>; <span>set</span><span>; } } </span><span>#endregion</span> <span>#region</span> DbColumn <span>///</span> <span><summary></summary></span> <span>///</span><span> 表字段结构 </span><span>///</span> <span></span> <span>public</span> <span>sealed</span> <span>class</span><span> DbColumn { </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 字段ID </span><span>///</span> <span></span> <span>public</span> <span>int</span> ColumnID { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 是否主键 </span><span>///</span> <span></span> <span>public</span> <span>bool</span> IsPrimaryKey { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 字段名称 </span><span>///</span> <span></span> <span>public</span> <span>string</span> ColumnName { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 字段类型 </span><span>///</span> <span></span> <span>public</span> <span>string</span> ColumnType { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 数据库类型对应的C#类型 </span><span>///</span> <span></span> <span>public</span> <span>string</span><span> CSharpType { </span><span>get</span><span> { </span><span>return</span><span> SqlServerDbTypeMap.MapCsharpType(ColumnType); } } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span><span> Type CommonType { </span><span>get</span><span> { </span><span>return</span><span> SqlServerDbTypeMap.MapCommonType(ColumnType); } } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 字节长度 </span><span>///</span> <span></span> <span>public</span> <span>int</span> ByteLength { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 字符长度 </span><span>///</span> <span></span> <span>public</span> <span>int</span> CharLength { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 小数位 </span><span>///</span> <span></span> <span>public</span> <span>int</span> Scale { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 是否自增列 </span><span>///</span> <span></span> <span>public</span> <span>bool</span> IsIdentity { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 是否允许空 </span><span>///</span> <span></span> <span>public</span> <span>bool</span> IsNullable { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span><span> 描述 </span><span>///</span> <span></span> <span>public</span> <span>string</span> Remark { <span>get</span>; <span>set</span><span>; } } </span><span>#endregion</span> <span>#region</span> SqlServerDbTypeMap <span>public</span> <span>class</span><span> SqlServerDbTypeMap { </span><span>public</span> <span>static</span> <span>string</span> MapCsharpType(<span>string</span><span> dbtype) { </span><span>if</span> (<span>string</span>.IsNullOrEmpty(dbtype)) <span>return</span><span> dbtype; dbtype </span>=<span> dbtype.ToLower(); </span><span>string</span> csharpType = <span>"</span><span>object</span><span>"</span><span>; </span><span>switch</span><span> (dbtype) { </span><span>case</span> <span>"</span><span>bigint</span><span>"</span>: csharpType = <span>"</span><span>long</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>binary</span><span>"</span>: csharpType = <span>"</span><span>byte[]</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>bit</span><span>"</span>: csharpType = <span>"</span><span>bool</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>char</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>date</span><span>"</span>: csharpType = <span>"</span><span>DateTime</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetime</span><span>"</span>: csharpType = <span>"</span><span>DateTime</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetime2</span><span>"</span>: csharpType = <span>"</span><span>DateTime</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetimeoffset</span><span>"</span>: csharpType = <span>"</span><span>DateTimeOffset</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>decimal</span><span>"</span>: csharpType = <span>"</span><span>decimal</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>float</span><span>"</span>: csharpType = <span>"</span><span>double</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>image</span><span>"</span>: csharpType = <span>"</span><span>byte[]</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>int</span><span>"</span>: csharpType = <span>"</span><span>int</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>money</span><span>"</span>: csharpType = <span>"</span><span>decimal</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>nchar</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>ntext</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>numeric</span><span>"</span>: csharpType = <span>"</span><span>decimal</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>nvarchar</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>real</span><span>"</span>: csharpType = <span>"</span><span>Single</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>smalldatetime</span><span>"</span>: csharpType = <span>"</span><span>DateTime</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>smallint</span><span>"</span>: csharpType = <span>"</span><span>short</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>smallmoney</span><span>"</span>: csharpType = <span>"</span><span>decimal</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>sql_variant</span><span>"</span>: csharpType = <span>"</span><span>object</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>sysname</span><span>"</span>: csharpType = <span>"</span><span>object</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>text</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>time</span><span>"</span>: csharpType = <span>"</span><span>TimeSpan</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>timestamp</span><span>"</span>: csharpType = <span>"</span><span>byte[]</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>tinyint</span><span>"</span>: csharpType = <span>"</span><span>byte</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>uniqueidentifier</span><span>"</span>: csharpType = <span>"</span><span>Guid</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>varbinary</span><span>"</span>: csharpType = <span>"</span><span>byte[]</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>varchar</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>case</span> <span>"</span><span>xml</span><span>"</span>: csharpType = <span>"</span><span>string</span><span>"</span>; <span>break</span><span>; </span><span>default</span>: csharpType = <span>"</span><span>object</span><span>"</span>; <span>break</span><span>; } </span><span>return</span><span> csharpType; } </span><span>public</span> <span>static</span> Type MapCommonType(<span>string</span><span> dbtype) { </span><span>if</span> (<span>string</span>.IsNullOrEmpty(dbtype)) <span>return</span><span> Type.Missing.GetType(); dbtype </span>=<span> dbtype.ToLower(); Type commonType </span>= <span>typeof</span>(<span>object</span><span>); </span><span>switch</span><span> (dbtype) { </span><span>case</span> <span>"</span><span>bigint</span><span>"</span>: commonType = <span>typeof</span>(<span>long</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>binary</span><span>"</span>: commonType = <span>typeof</span>(<span>byte</span>[]); <span>break</span><span>; </span><span>case</span> <span>"</span><span>bit</span><span>"</span>: commonType = <span>typeof</span>(<span>bool</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>char</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>date</span><span>"</span>: commonType = <span>typeof</span>(DateTime); <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetime</span><span>"</span>: commonType = <span>typeof</span>(DateTime); <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetime2</span><span>"</span>: commonType = <span>typeof</span>(DateTime); <span>break</span><span>; </span><span>case</span> <span>"</span><span>datetimeoffset</span><span>"</span>: commonType = <span>typeof</span>(DateTimeOffset); <span>break</span><span>; </span><span>case</span> <span>"</span><span>decimal</span><span>"</span>: commonType = <span>typeof</span>(<span>decimal</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>float</span><span>"</span>: commonType = <span>typeof</span>(<span>double</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>image</span><span>"</span>: commonType = <span>typeof</span>(<span>byte</span>[]); <span>break</span><span>; </span><span>case</span> <span>"</span><span>int</span><span>"</span>: commonType = <span>typeof</span>(<span>int</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>money</span><span>"</span>: commonType = <span>typeof</span>(<span>decimal</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>nchar</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>ntext</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>numeric</span><span>"</span>: commonType = <span>typeof</span>(<span>decimal</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>nvarchar</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>real</span><span>"</span>: commonType = <span>typeof</span>(Single); <span>break</span><span>; </span><span>case</span> <span>"</span><span>smalldatetime</span><span>"</span>: commonType = <span>typeof</span>(DateTime); <span>break</span><span>; </span><span>case</span> <span>"</span><span>smallint</span><span>"</span>: commonType = <span>typeof</span>(<span>short</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>smallmoney</span><span>"</span>: commonType = <span>typeof</span>(<span>decimal</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>sql_variant</span><span>"</span>: commonType = <span>typeof</span>(<span>object</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>sysname</span><span>"</span>: commonType = <span>typeof</span>(<span>object</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>text</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>time</span><span>"</span>: commonType = <span>typeof</span>(TimeSpan); <span>break</span><span>; </span><span>case</span> <span>"</span><span>timestamp</span><span>"</span>: commonType = <span>typeof</span>(<span>byte</span>[]); <span>break</span><span>; </span><span>case</span> <span>"</span><span>tinyint</span><span>"</span>: commonType = <span>typeof</span>(<span>byte</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>uniqueidentifier</span><span>"</span>: commonType = <span>typeof</span>(Guid); <span>break</span><span>; </span><span>case</span> <span>"</span><span>varbinary</span><span>"</span>: commonType = <span>typeof</span>(<span>byte</span>[]); <span>break</span><span>; </span><span>case</span> <span>"</span><span>varchar</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>case</span> <span>"</span><span>xml</span><span>"</span>: commonType = <span>typeof</span>(<span>string</span>); <span>break</span><span>; </span><span>default</span>: commonType = <span>typeof</span>(<span>object</span>); <span>break</span><span>; } </span><span>return</span><span> commonType; } } </span><span>#endregion</span><span> #</span>></datarow></dbcolumn></dbtable>
<span><span>#@ include </span><span>file</span><span>="$(ProjectDir)DbHelper.ttinclude"</span><span> #</span><span>></span></span>
DbHelper相对比较复杂,把一些常用操作进行了简单封装,因此放到一个单独的文件里面进行引用,可以方便的进行复用,这里DbHelper的后缀名使用ttinclude,这里的后缀名可以随便起,按照微软的建议:用于include的文件尽量不要使用.tt做后缀名
public <span>class</span><span> config { </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> ConnectionString=<span>"</span><span>Data Source=(local);Integrated Security=true;Initial Catalog=Northwind;</span><span>"</span><span>; </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> DbDatabase=<span>"</span><span>Northwind</span><span>"</span><span>; </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> TableName=<span>"</span><span>Customers</span><span>"</span><span>; } #</span>>
这里我们把数据库连接串和数据库、表名字定义一下,方便修改和使用
<span>//</span><span>------------------------------------------------------------------------------ </span><span>//</span><span> <auto-generated> </auto-generated></span><span>//</span><span> 此代码由T4模板自动生成 </span><span>//</span><span> 生成时间 by 懒惰的肥兔 </span><span>//</span><span> 对此文件的更改可能会导致不正确的行为,并且如果 </span><span>//</span><span> 重新生成代码,这些更改将会丢失。 </span><span>//</span><span> </span><span>//</span><span>------------------------------------------------------------------------------</span> <span>using</span><span> System; </span><span>namespace</span><span> T4ConsoleApplication.Entities { </span><span>public</span> <span>class</span> <span> { </span>foreach(DbColumn column <span>in</span> DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, config.TableName)){#> <span>///</span> <span><summary></summary></span> <span>///</span> <span></span> <span>///</span> <span></span> <span>public</span> if(column.CommonType.IsValueType && column.IsNullable){#>? { <span>get</span>; <span>set</span><span>; } </span><span> } }</span>
Customers.tt
"false" hostspecific="false" language="C#" #> ".cs" #> "System.Core.dll" #> "System.Data.dll" #> "System.Data.DataSetExtensions.dll" #> "System.Xml.dll" #> namespace="System" #> namespace="System.Xml" #> namespace="System.Linq" #> namespace="System.Data" #> namespace="System.Data.SqlClient" #> namespace="System.Collections.Generic" #> namespace="System.IO" #> "$(ProjectDir)DbHelper.ttinclude" #> //------------------------------------------------------------------------------ //// 此代码由T4模板自动生成 // 生成时间 by 懒惰的肥兔 // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // //------------------------------------------------------------------------------ using System; namespace T4ConsoleApplication.Entities { public class { foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, config.TableName)){#> /// /// /// public if(column.CommonType.IsValueType && column.IsNullable){#>? { get; set; } } } public <span>class</span><span> config { </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> ConnectionString=<span>"</span><span>Data Source=(local);Integrated Security=true;Initial Catalog=Northwind;</span><span>"</span><span>; </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> DbDatabase=<span>"</span><span>Northwind</span><span>"</span><span>; </span><span>public</span> <span>static</span> <span>readonly</span> <span>string</span> TableName=<span>"</span><span>Customers</span><span>"</span><span>; } #</span>>
进行保存后会自动生成Customers.cs文件
Customers.cs
<span>//</span><span>------------------------------------------------------------------------------ </span><span>//</span><span> <auto-generated> </auto-generated></span><span>//</span><span> 此代码由T4模板自动生成 </span><span>//</span><span> 生成时间 2012-07-18 17:51:26 by 懒惰的肥兔 </span><span>//</span><span> 对此文件的更改可能会导致不正确的行为,并且如果 </span><span>//</span><span> 重新生成代码,这些更改将会丢失。 </span><span>//</span><span> </span><span>//</span><span>------------------------------------------------------------------------------</span> <span>using</span><span> System; </span><span>namespace</span><span> T4ConsoleApplication.Entities { </span><span>public</span> <span>class</span><span> Customers { </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> CustomerID { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> CompanyName { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> ContactName { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> ContactTitle { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> Address { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> City { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> Region { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> PostalCode { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> Country { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> Phone { <span>get</span>; <span>set</span><span>; } </span><span>///</span> <span><summary></summary></span> <span>///</span> <span>///</span> <span></span> <span>public</span> <span>string</span> Fax { <span>get</span>; <span>set</span><span>; } } }</span>
至此完整演示了怎样一步步根据数据库生成实体类的操作,是不是很简单,如对语法和操作不理解的地方可以参考T4模版引擎之基础入门,稍微用心研究下,轻松打造属于自己的代码生成器。
通过单个T4模板生成多个文件,以及自动生成整个数据库的所有实体类,敬请期待
下班了,拍拍屁股走人。。。
源码:T4ConsoleApplication.rar