CodeSmith生成三层代码之实体层(1)

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="请选择数据表!" %>
<%@ Property Name="NameSpace" Type="System.String" Default="" Optional="False" Category="Description" Description="请输入命名空间" %>
<%@ Property Name="Author" Type="System.String" Default="Pantao" Optional="False" Category="Description" Description="About Author" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

/*************************************************************/
/* CodeSmith模板生成实体类 */
/*************************************************************/
using System;
namespace <%=NameSpace%>.Model
{
/// <summary>
/// <%=SourceTable.Description%>
/// </summary>
public class <%=SourceTable.Name%>
{
<% for(int i=0; i<SourceTable.Columns.Count; i++) { %>
/// <summary>
/// <%=SourceTable.Columns[i].Description%>
/// </summary>
public <%=GetSqlParameterStatement(SourceTable.Columns[i]) + " " + SourceTable.Columns[i].Name%> {get; set;}
<%}%>
}
}

<script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
{
string para = "";
if (column.Name.EndsWith("TypeCode"))
return column.Name;
switch (column.DataType)
{
case DbType.AnsiString:
para = "string";
break;
case DbType.AnsiStringFixedLength:
para = "string";
break;
case DbType.Binary:
para = "byte[]";
break;
case DbType.Boolean:
para = "bool";
break;
case DbType.Byte:
para = "int";
break;
case DbType.Currency:
para = "decimal";
break;
case DbType.Date:
para = "DateTime";
break;
case DbType.DateTime:
para = "DateTime";
break;
case DbType.Decimal:
para = "decimal";
break;
case DbType.Double:
para = "double";
break;
case DbType.Guid:
para = "Guid";
break;
case DbType.Int16:
para = "short";
break;
case DbType.Int32:
para = "int";
break;
case DbType.Int64:
para = "long";
break;
case DbType.Object:
para = "object";
break;
case DbType.SByte:
para = "sbyte";
break;
case DbType.Single:
para = "float";
break;
case DbType.String:
para = "string";
break;
case DbType.StringFixedLength:
para = "string";
break;
case DbType.Time:
para = "TimeSpan";
break;
case DbType.UInt16:
para = "ushort";
break;
case DbType.UInt32:
para = "uint";
break;
case DbType.UInt64:
para = "ulong";
break;
case DbType.VarNumeric:
para = "decimal";
break;
default:
para = "__UNKNOWN__" + column.NativeType;
break;
}
if(para == "string")
return para;
if(column.AllowDBNull)
para = para + "?";
return para;
}
</script>

你可能感兴趣的:(C++,c,C#)