背景:上学那会儿就接触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备份在自己的电脑上,天啊,覆盖了,我的数据你快回来...

CodeSmith Template Model Output的更多相关文章

  1. 完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案

    问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写MySQL模板的时候,会发现一个问题:MySQL数据表中的列说明获取不到,也就是column ...

  2. CodeSmith批量生成实体

    保存以下文件为ModelBatch.cst <%@ Register Name="Model" Template="D:\Q\web\LHWYVISIT\trunk ...

  3. CodeSmith

    完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案   问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写 ...

  4. Deploying Keras model on Tensorflow Serving--

    keras训练了个二分类的模型.需求是把keras模型跑到 tensorflow serving上 (TensorFlow Serving 系统用于在生产环境中运行模型) keras模型转 tenso ...

  5. template 的使用

    插件介绍:template 是一个高性能的JavaScript模板引擎. 插件特性: 1.性能卓越,执行速度快(mustache 与 tmpl 的20多倍): 2.支持运行时调试,可精准定位异常模板所 ...

  6. DeepLearning - Overview of Sequence model

    I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...

  7. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  8. Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  9. spring源码分析之freemarker整合

    FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个Java类库,是一款程 ...

随机推荐

  1. MEF 编程指南(九):部件生命周期

    理解 MEF 容器部件生命周期和实现是非常重要的事情.考虑到 MEF 关注可扩展应用程序.这变得尤为重要.生命期可以解释为期望部件的共享性(transitively, its exports)   共 ...

  2. 我所经历的SAP选型

    这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...

  3. android访问webservices

    /** * 手机号段归属地查询(模拟器.HTC 可以) *  * @param phoneSec 手机号段 */ public  void getRemoteInfo() { /*String pho ...

  4. Lua学习教程之 可变參数数据打包与解包

    利用table的pack与unpack进行数据打包与解包.測试代码例如以下: print("Test table.pack()----------------"); functio ...

  5. PhoneTutorial

    https://github.com/navasmdc/PhoneTutorial PhoneTutorial-master.zip

  6. oc-10-函数与方法的区别

    .函数和对象方法的区别 以-开头的方法就是对象方法(即必须实例化对象才能使用的方法) 如: -(void)Run; 区别: ()语法区别,并且对象方法都以-号开头,函数直接以返回值开头 ()对象方法的 ...

  7. dsp与sem的互补以及技术实现

    SEM翻译过来叫搜索引擎营销,个人认为是随着搜索引擎竞价排名出现的一个行业,已经有了好多年的历史,做sem的公司这些年里手里应该都积攒着大量的cookie,关键词等与SEM和追踪相关的数据,这些数据现 ...

  8. svm、经验风险最小化、vc维

    原文:http://blog.csdn.net/keith0812/article/details/8901113 “支持向量机方法是建立在统计学习理论的VC 维理论和结构风险最小原理基础上” 结构化 ...

  9. redis终于有比较大的进展了,redis3.0.1 稳定版本发布,支持集群。

    原文地址:https://raw.githubusercontent.com/antirez/redis/3.0/00-RELEASENOTES Redis 3.0 release notes --[ ...

  10. 使用hibernate自动创建Mysql表失败原因

    1.使用 schemaExport(hibernate5.2.3)创建Mysql表 @Test public void testSchemaExport() { ServiceRegistry ser ...