CodeSmith 创建Ado.Net自定义模版(二)

CodeSmith 创建Ado.Net自定义模版(二)


建立第二个C# Template: Step2_Model.cst(实体类模版)

 http://www.dtan.so

代码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="实体类" %>
<%@ Property Name="NameSpace" Type="System.String" Default="Model" Category="Property" Description="命名空间" %>
<%@ Property Name="Author" Type="System.String" Default="Wilson" Category="Property" Description="作者名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表映射文件" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>

<script runat="template">
    /// <summary>
    ///把数据库类型转化为C#类型
    /// </summary>
    public string DataType2CSharpType(System.Data.DbType dbType)
    {
        switch (dbType)
        {
            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.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                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 "DateTime";                
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }
</script>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace <%=NameSpace%>
{
    /// <summary>
    /// 功能: 实体类 (<%=SourceTable.Description%>)
    /// 创建人:<%=Author%>     
    /// 创建日期:<%=DateTime.Now.ToShortDateString() %>    
    /// </summary>
    [Serializable]
    public class <%=SourceTable.Name%>
    {
        public <%=SourceTable.Name%>()
        {
			
        }

       #region##<%=SourceTable.Name%>实体

       <%for(int i = 0;i < SourceTable.Columns.Count;i++)%>
	   <%{%>
	
	   /// <summary>
       /// <%=SourceTable.Columns[i].Description%>
       /// </summary>
	   public <%=DataType2CSharpType(SourceTable.Columns[i].DataType)%> <%=SourceTable.Columns[i].Name%> {get; set;}
	
	   <%}%>
	   
	   #endregion
    }
}

DataType2CSharpType方法是把数据库的类型转化为C#类型。。。


很简单,几个标签己经在CodeSmith 创建Ado.Net自定义模版(一)中做过介绍


<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表映射文件" %>


有这句话,在属性栏中可以看到db大目录下,SourceTable属性,属性框中有个按钮,点击,会弹出选择数据库的窗口


或者先在工具的Scheme Explorer工具栏中,点击ManagerDataSource,添加一个数据库进来。。


这样你就可以选择数据库表


<%=SourceTable.Name%>  :这是你选择的表名

<%=SourceTable.Description%> :这是你数据库中对表的描述

SourceTable.Columns.Count :这是数据库中的列总数

SourceTable.Columns[i].DataType :是索引为i的列类型

SourceTable.Columns[i].Name  : 是索引用i的列名


PS:CodeSmith Studio中有智能提示,其它属性,大家可以自己试试


示例下载地址:下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值