如何使用CodeSmith批量生成代码

helloworld.cst文件的内容

--------------------------------------开始-----------------------------------------

 

<%--
Name:helloworld
Author: 乔
Description:练习使用
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. GettingStarted - Required" Description="Database that the tables views, and storedprocedures should be based on. IMPORTANT!!! If SourceTables and SourceViews areleft blank, the Entire Database will then be generated." %>

<%-- 注册实体层Entity模板 --%>
<%@ Register Name="EntityTemplate" Template="Entity.cst" MergeProperties="Flase" ExcludeProperties="" %>


<script runat="template">
    //解决方案输出路径
    private string Directory = String.Empty;
   
    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
    [Optional, NotChecked]
    [DefaultValue("")]
    public string OutputDirectory
    {
        get
        {
            return Directory;
        }
        set
        {
            if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
            Directory = value;
        }
    }
</script>


<script runat="template">
    //生成实体Entity类
    private void GenerateEntityClasses()
    {
        CodeTemplate Template = new EntityTemplate();
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            string FileDirectory = OutputDirectory + "\\"  + table.Name + ".cs";
            //生成模板
            Template.SetProperty("Table",table);
            //文件输出
            Template.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory +" 创建成功.");
        }
    }
</script>
<%
//创建实体层Entity类
this.GenerateEntityClasses();
Debug.WriteLine("OK");
%>

 

 

-------------------------------------结束-----------------------------------------------------------

 

Entity.cst文件内容

 

 

------------------------开始---------------------------------------------

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%-- 要打印的表 --%>
<%@ Property Name="Table" DeepLoad="True" Type="TableSchema" Optional="False" Category="01. Getting Started - Required"Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>

<%--测试版本,要是使用需要修改 --%>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Entity
{
    public partial class <%= Table.Name%>
    {
        <% foreach(ColumnSchema col in Table.Columns){ %>
        public <%= col.DataType %> <%=col.Name %>
  {
   get;
   set;
  }
        <% } %>
    }
}

 

------------------------结束---------------------------------------

原文地址:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html

你可能感兴趣的:(String,C#,assembly,table,database,import)