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备份在自己的电脑上,天啊,覆盖了,我的数据你快回来...

时间: 2024-10-05 05:50:19

CodeSmith Template Model Output的相关文章

4、概率图模型:Template Modles

本章总结几个模板模型:动态贝叶斯.隐马尔科夫和Plate模型 1 OverView of Template Models 为了达到不用每一都对问题建立模型,而是每次都对某一类问题有一个统一的模型进行处理,而提出了模板模型.模板模型(template model)编码了具有重复结构和共享参数的模型.从而使图模型的描述方式更加的紧凑,可以应用于无限大的贝叶斯网络.主要包括模板变量(Template variables)和语言(language). 模板变量(Template variables)是图

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 Posted by Oleg Sych January 1, 2009 T4 is a fully-featured, template-based code generation engine built into Visual Studio 2008. It offers rich function

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 Posted by Oleg Sych January 1, 2009 T4 is a fully-featured, template-based code generation engine built into Visual Studio 2008. It offers rich function

使用T4模板生成代码的学习

之前做项目使用的都是Db First,直接在项目中添加Entity Framework,使用T4模板(T4模板引擎之基础入门)生成DAL BLL层等(T4模板是一个同事给的,也没有仔细研究,代码如下:) <#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"

A Look at the Razor View Engine in ASP.NET MVC

The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the neat separation between the two key phases of processing the request and generating the response. In general, rendering an ASP.NET Web Forms page means

IOS 中的JS

 文章摘自: http://www.cocoachina.com/ios/20150127/11037.html  JSContext/JSValue JSContext 即JavaScript代码的运行环境.一个Context就是一个JavaScript代码执行的环境,也叫作用域.当在浏览器中运行 JavaScript代码时,JSContext就相当于一个窗口,能轻松执行创建变量.运算乃至定义函数等的JavaScript //Objective-C JSContext *context = [

Nancy - 视图引擎

The concept of a view engine is quite simple - it takes a “template” and an optional “model” (the data) and outputs (usually) HTML to be rendered into the browser. By default, Nancy ships with a built in view engine known as the SuperSimpleViewEngine

Effective Tensorflow[转]

Effective TensorFlow Table of Contents TensorFlow Basics Understanding static and dynamic shapes Scopes and when to use them Broadcasting the good and the ugly Feeding data to TensorFlow Take advantage of the overloaded operators Understanding order

TensorRT&amp;Sample&amp;Python[uff_custom_plugin]

本文是基于TensorRT 5.0.2基础上,关于其内部的uff_custom_plugin例子的分析和介绍. 本例子展示如何使用cpp基于tensorrt python绑定和UFF解析器进行编写plugin.该例子实现一个clip层(以CUDA kernel实现),然后封装成一个tensorrt plugin,然后生成一个动态共享库,用户可以动态的在python中链接该库,将该plugin注册到tensorrt的plugin registry中,并让UFF解析器能够使用. 该例子还是有些知识点