• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

codesmith自动生成C#model实体模板

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<%-- 
Name:自动生成
Author: 陈胜威
Description: 直接生成model类
--%>
<%@ Template Language="C#" TargetLanguage="cs" %>  
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Default="" Optional="True" Category="Tables" Description="This is a sample string property." %>  
<%@ Property Name="ProjectName" Type="System.string" Default="Blog" Optional="False" Category="Strings" Description="This is Project Name" %>  
<%@ Assembly Name="SchemaExplorer" %>  
<%@ Import Namespace="SchemaExplorer" %>  
<%@ Assembly Name="SchemaExplorer" %>  
<%@ Assembly Name="System.Data" %>  
<%@ Import Namespace="SchemaExplorer" %>  
<%@ Import Namespace="System.Data" %>  
<%@ Map Name="SqlCSharp" Src="Sql-CSharp" Description="System to C# Type Map" %>  
<%@ Map Name="DbDataReader" Src="DbType-DataReaderMethod" Description="DbType to DataReader Method Map" %>  
<%@ Map Name="SqlNativeSqlDb" Src="SqlNativeType-SqlDbType" Description="SqlNativeType to SqlDbType Map" %>  
<%@ Map Name="DbTypeCSharp" Src="DbType-CSharp" Description="DbType to CSharp Map" %>  
  
  
using System;  
  
namespace <%= ProjectName%>.Model  
{  
    public class <%= GetClassName(SourceTable) %>  
    {  
         <%  
              foreach(var column in SourceTable.Columns)  
              {  
     %>  
       
         <%=GetMemberVariableDeclarationStatement(column)%>  
       
     <%    
              }  
        %>  
        public <%= GetClassName(SourceTable) %>(<%=GetConStructorParameters(SourceTable) %>)  
        {  
        <%  
              foreach(ColumnSchema column in SourceTable.Columns)  
              {  
         %>  
         <%= GetMemberVariableName(column) %>=<%= GetMemberVariableName(column).Substring(1) %>;  
           
         <%  
              }  
        %>  
        }  
          
        <%  
                
        foreach(ColumnSchema column in SourceTable.Columns)  
        {  
         %>  
           
         public <%=GetCSharpVariableType(column) %> <%=GetPascalMemberVariableName(column) %>  
         {  
            get  
            {  
              
             return <%=GetMemberVariableName(column) %>;  
              
            }  
            set  
            {  
              
              <%=GetMemberVariableName(column) %>=value;  
             
            }  
           
         }  
           
         <%  
        }  
        %>  
    }  
}  
    
<script runat="template">  
public string GetConStructorParameters(TableSchema table)  
{  
    string returnParameters="";  
    foreach(ColumnSchema column in table.Columns)  
    {  
       returnParameters+=GetCSharpVariableType(column)+" "+GetMemberVariableName(column).Substring(1)+",";  
    }  
    return returnParameters.Substring(0,returnParameters.Length-1);  
}  
      
public string GetMemberVariableDeclarationStatement(ColumnSchema column)  
{  
    return GetMemberVariableDeclarationStatement("private", column);  
}  
  
public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column)  
{  
    string statement = protectionLevel + " ";  
    statement += GetCSharpVariableType(column) + " " + GetMemberVariableName(column);  
      
    string defaultValue = GetMemberVariableDefaultValue(column);  
    if (defaultValue != "")  
    {  
        statement += " = " + defaultValue;  
    }  
      
    statement += ";";  
      
    return statement;  
}  
  
public string GetReaderAssignmentStatement(ColumnSchema column, int index)  
{  
    string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";  
    statement += GetMemberVariableName(column) + " = ";  
      
    if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";  
      
    statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";  
      
    return statement;  
}  
  
public string GetCamelCaseName(string value)  
{  
    return value.Substring(0, 1).ToLower() + value.Substring(1);  
}  
  
public string GetMemberVariableName(ColumnSchema column)  
{  
    string propertyName = GetPropertyName(column);  
    string memberVariableName = "_" + GetCamelCaseName(propertyName);  
      
    return memberVariableName;  
}  
  
public string GetPascalMemberVariableName(ColumnSchema column)  
{  
    string propertyName = GetPropertyName(column);  
    string memberVariableName = "_" + GetCamelCaseName(propertyName);  
      
    string temp= propertyName.Substring(0,1).ToUpper()+propertyName.Substring(1);  
    return temp;  
}  
      
public string GetPropertyName(ColumnSchema column)  
{  
    string propertyName = column.Name;  
      
    if (propertyName == column.Table.Name + "Name") return "Name";  
    if (propertyName == column.Table.Name + "Description") return "Description";  
      
    if (propertyName.EndsWith("TypeCode")) propertyName = propertyName.Substring(0, propertyName.Length - 4);  
      
    return propertyName;  
}  
          
public string GetMemberVariableDefaultValue(ColumnSchema column)  
{  
    switch (column.DataType)  
    {  
        case DbType.Guid:  
        {  
            return "Guid.Empty";  
        }  
        case DbType.AnsiString:  
        case DbType.AnsiStringFixedLength:  
        case DbType.String:  
        case DbType.StringFixedLength:  
        {  
            return "String.Empty";  
        }  
        default:  
        {  
            return "";  
        }  
    }  
}  
  
public string GetCSharpVariableType(ColumnSchema column)  
{  
    if (column.Name.EndsWith("TypeCode")) return column.Name;  
  
    return DbTypeCSharp[column.DataType.ToString()];      
}  
  
public string GetReaderMethod(ColumnSchema column)  
{  
    return DbDataReader[column.DataType.ToString()];  
}  
  
public string GetClassName(TableSchema table)  
{  
    if (table.Name.EndsWith("s"))  
    {  
        return table.Name.Substring(0, table.Name.Length - 1);  
    }  
    else  
    {  
        return table.Name;  
    }  
}  
  
public string GetSqlDbType(ColumnSchema column)  
{  
    return SqlNativeSqlDb[column.NativeType.ToString()];  
}  
  
public string GetPrimaryKeyType(TableSchema table)  
{  
    if (table.PrimaryKey != null)  
    {  
        if (table.PrimaryKey.MemberColumns.Count == 1)  
        {  
            return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);  
        }  
        else  
        {  
            throw new ApplicationException("This template will not work on primary keys with more than one member column.");  
        }  
    }  
    else  
    {  
        throw new ApplicationException("This template will only work on tables with a primary key.");  
    }  
}  
  
public override string GetFileName()  
{  
    return this.GetClassName(this.SourceTable) + ".cs";  
}  
</script>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap