背景:上学那会儿就接触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. cdoj 1256 昊昊爱运动 预处理/前缀和

    昊昊爱运动 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1256 Descr ...

  2. delphi 08 HTML组件

    ///HTML组件///后面的字符串为这个控件的ID号///直线          Line         (WebBrowser1.Document as IHTMLDocument2).exec ...

  3. 通过GitHub和Hexo搭建个人博客

    LinEvan个人博客 最终有自己的个人博客,逼格一下子提高说不少. 网上一搜教程一大堆,非常多已经写得非常好了,我就不凑这个热闹了.推荐一篇博文:怎样搭建一个独立博客--简明Github Pages ...

  4. 如何通过VIM把代码格式化后生成HTML网页代码

    本文转自http://wangxiaoyu.blog.51cto.com/922065/203471 需求及思路:演示需要,需要网站上嵌入一些代码,我的建议做法是根据代码文件,生成相应的HTML代码, ...

  5. Samba服务器配置参考链接

    一步一学Linux与Windows共享文件Samba(很适合初学者,极力推荐): http://os.51cto.com/art/200709/56395.htm 由最简单的一个例子说起,匿名用户可读 ...

  6. mysql索引需要了解的几个注意

    板子之前做过2年web开发培训(入门?),获得挺多学生好评,这是蛮有成就感的一件事,准备花点时间根据当时的一些备课内容整理出一系列文章出来,希望能给更多人带来帮助,这是系列文章的第一篇 注:科普文章一 ...

  7. Subsequence

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  8. Android ExpandableListView的技巧和问题

    前言: 最近一个多月在认真的学习Android和做项目,文章内容表达的不好或者理解错了,希望大家评论指出. :-) 本文是总结几个比较常用且使用的技巧,和一个大家都会遇到的问题. 文章中大部分语句摘抄 ...

  9. C++之类的构造函数,不得不学明白的重点

    一.认识构造函数 当创建一个类类型对象时,类通过一个或者几个特殊的成员函数来控制对象的初始化,这种函数就是构造函数.它的任务就是用来初始化类对象的成员的,所以当创建类对象或者类对象被创建就会调用构造函 ...

  10. CF Fox And Two Dots (DFS)

    Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...