背景:上学那会儿就接触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. 如何用boost::serialization去序列化派生模板类(续)

    在 如何用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类, 在写測试用例时一直出现编译错误,调了非常久也没跳出来,今天偶然试了一下...竟然调了出来. ...

  2. TC SRM 664 div2 A BearCheats 暴力

     BearCheats Problem Statement    Limak is an old brown bear. Because of his bad eyesight he sometime ...

  3. cocos2d-x 资源路径研究

    调用cc.FileUtils:getInstance():addSearchResolutionsOrder("src"); 加入�一个搜索路径,就能够直接载入src文件夹下的资源 ...

  4. MAC SVN Phonegap

    1. Windows上用VisualSVN Server Manager创建好Repository. 2. 在MAC上,用Phonegap创建好项目,比如在Project1目录里的App目录. 3. ...

  5. Cent OS安装TL-WN725N 2.0 USB驱动

    TP Link官方没有提供TL-WN725N 2.0的Linux驱动下载,折腾了我半天,试了各种方法.也有一部分原因是因为这机器还不能联网,导致有一些驱动因为缺少依赖并不成功安装. 后来终于在gith ...

  6. C#开发---利用特性自定义数据导出到Excel

    网上C#导出Excel的方法有很多.但用来用去感觉不够自动化.于是花了点时间,利用特性做了个比较通用的导出方法.只需要根据实体类,自动导出想要的数据  1.在NuGet上安装Aspose.Cells或 ...

  7. 3905 - Meteor

    The famous Korean internet company nhn has provided an internet-based photo service which allows The ...

  8. nodejs的mysql模块学习(四)断开数据库连接

    断开连接有两种方式 end()函数 在这种情况下 所有先前排队的查询 仍然可以继续继续发送到服务器,但是如果在执行到断开连接的命令之前发生了致命的错误,那么end()将不会被执行 connection ...

  9. PHP+ MongoDB

    环境: uname -v # SMP Debian -+deb7u2 php -v PHP -~dotdeb. (cli) (built: Jun ::) Copyright (c) - The PH ...

  10. 在Code first中使用数据库里的视图

    如果想在Code first中使用数据库里的视图 (不管你出于什么原因),目前的方法有2种. 一.使用Database.SqlQuery<T>("查询语句"),如: v ...