CodeSmith Template Model Output

背景:上学那会儿就接触CodeSmith,是一款非常优秀的代码自动生成工具。以前写过好些基本模版,可惜早不知道扔到哪儿去了,如今只能重新开始,把它捡回来,加油。

效果:将数据库 DataBase 应用到Model模版导出生成基于该库的所有实体 Entity 保存在本地,便于后期开发使用,工具下载 http://pan.baidu.com/s/1o6n2Z94

其中细节有些是拷贝网上的资料,再改过的,仅供学习参考,版权问题,概不负责

代码分享:Model.cst

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer"%>
<%@ Import Namespace="SchemaExplorer"%>

<%@ Property Name="Table" Type="TableSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Entities
{
    public class <%=StringUtil.ToPascalCase(Table.Name)%>
    {
    
        <%foreach(ColumnSchema column in Table.Columns){ %>
        public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %> { get;set; }
        
        <% } %>
    }
}

 

Output.cst

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>


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


<%-- 数据库 --%>
<%@ Property Name="SourceDatabase" Default="WBV1DB" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%>


<%
//创建实体层Entity类
this.GenerateEntityClasses();

Debug.WriteLine("OK");
%>

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

<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>

 

其它:

  使用MySql引擎 需要将MySql.Data.dll http://pan.baidu.com/s/1bn2c7ef 放入C:\Program Files (x86)\CodeSmith\v5.0\SchemaProviders (默认安装目录)

  MySql Connertion String  Database=ysendb;Data Source=127.0.0.1;User Id=root;Password=******

  SqlServer Connertion String  Data Source=.;Initial Catalog=WBV1DB;Persist Security Info=True;User ID=sa;Password=******

总结:个人有用的资料,案例,数据请牢记备份啊,丢失都是一笔巨大的损失!备份也请注意,哎呀,服务器重装的时候把一个DB备份在自己的电脑上,天啊,覆盖了,我的数据你快回来...

posted @ 2015-02-11 10:57  chenyj  阅读(414)  评论(0编辑  收藏  举报