作者:
来源:
声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。
转自:http://www.cnblogs.com/youring2/archive/2012/06/20/2556460.html
本文共 2850 字,大约阅读时间需要 9 分钟。
Brad为我们提供了T4模板,因为公司一直在使用CodeSmith,故为其写了一个CodeSmith的模板,代码如下:
<%-- Name:EntityTemplatesAuthor:Qi Fei Description:Generate a entity file in C#--%><%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="" ResponseEncoding="UTF-8" %><%@ Property Name="Namespace" Type="System.String" Default="TianChenMeiKuang.Entity" Optional="False" Category="Strings" Description="实体类命名空间" %><%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="源表" %><%@ Assembly Name="SchemaExplorer" %><%@ Assembly Name="System.Data" %><%@ Import Namespace="SchemaExplorer" %><%@ Import Namespace="System.Data" %>/********************************************************** Name:<%= GetClassName(SourceTable) %> Author: Date:<%=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") %> Description: Modify Remark:**********************************************************/using System;using WebApp.Matrix.Data;namespace <%=Namespace%>{ ////// This Entity is Mapping To [<%=SourceTable.Name%>] Table /// Remark Ignore Attribute for the field when it is not need mapping /// [Serializable] [TableName("[<%=SourceTable.Name%>]")] <% ColumnSchema primaryKeyColumn = GetPrimaryKeyColumn(); if(primaryKeyColumn != null) { if(Convert.ToBoolean(primaryKeyColumn.ExtendedProperties["CS_isIdentity"].Value)==true){ %> [PrimaryKey("<%=primaryKeyColumn.Name%>", autoIncrement=true)] <% } else { %> [PrimaryKey("<%=primaryKeyColumn.Name%>", autoIncrement=false)] <% } }%> public class <%= GetClassName(SourceTable) %> { <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %> ////// <%= SourceTable.Columns[i].Name %> /// public <%= GetCSharpVariableType(SourceTable.Columns[i]) %> <%= GetPropertyName(SourceTable.Columns[i]) %> { get; set; } <% if (i < SourceTable.Columns.Count - 1) Response.Write("\r\n"); %> <% } %> ////// Equals /// public override bool Equals(object obj) { <%= GetClassName(SourceTable) %> other = obj as <%= GetClassName(SourceTable) %>; if (<%=GetFirstKeyCondition()%>) { return false; } if (<%=GetTwoKeyCondition()%>) { return false; } return true; } ////// GetHashCode /// public override int GetHashCode() { return base.GetHashCode(); } }}
该模板只适用于但主键的环境,且主键必须为字符串类型,或者为自增长列。初步满足公司当前的需要。
如果认为此文对您有帮助,别忘了支持一下哦!