CodeSmith生成实体层代码

<%--
Name:dd
Author:binbin    
Description: booksir
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Property   Name="SourceTable"   Type="SchemaExplorer.TableSchema"    Category="Context"   Description="选择一个表" %>
<%@ Property Name="NameSpace" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="程序的命名空间" %>
using System;
using System.Web;
using System.Data;
namespace <%=NameSpace%>
{
    public class <%=OneToUpper(SourceTable.Name)%>Info
    {
        <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
        private <%=GetCSharpVariableType(SourceTable.Columns[i])%> _<%=OneToLower(SourceTable.Columns[i].Name)%>;
        <%}%>

        <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
        public <%=GetCSharpVariableType(SourceTable.Columns[i])%> <%=OneToUpper(SourceTable.Columns[i].Name)%>
        {
            get{return _<%=OneToLower(SourceTable.Columns[i].Name)%>;}
   set {_<%=OneToLower(SourceTable.Columns[i].Name)%> = value;}
        }
        <%}%>
    }
   
}
<script runat="template">
//第一个字母大写
private string OneToUpper(string str)
{
    return str.Substring(0,1).ToUpper()+str.Substring(1);
}
//第一个字母小写
private string OneToLower(string str)
{
    return str.Substring(0,1).ToLower()+str.Substring(1);   
}

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

你可能感兴趣的:(code)