自定义t4模版,根据数据库生成实体模型

简介: Template文件

Template文件

<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ import namespace="System" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>

<#+
public class ProcedureTemplate : Template
{
    public string DatabaseName {get; private set;}
	public string TableName{get;private set;}
	public Table Table {get; private set;}
	
	public const string VersionNoField 			= "VERSION_NO";
	public const string VersionNoParameterName 	= "@p_version_no";
	
	public ProcedureTemplate(string databaseName, string tableName)
	{
		this.DatabaseName 	= databaseName;
		this.TableName = tableName;
	}
	
	public virtual string GetProcedureName()
	{
		return this.TableName + "M";	
	}

    public override string TransformText()
    { 
		Server server = new Server();
        Database database = new Database(server, DatabaseName);
        var table = new Table(database, TableName);
        table.Refresh();
		WriteLine("using System;");
		WriteLine("using System.Runtime.Serialization;");
		WriteLine("namespace RTMDemo.Model");
		WriteLine("{");
		PushIndent("\t");
		WriteLine("[DataContract]");
		WriteLine("public class {0}M",TableName);
		WriteLine("{");
		PushIndent("\t");
		for(int i=0; i<table.Columns.Count;i++)
		{
			Column column = table.Columns[i];
			if(column.Name != VersionNoField)
			{
				WriteLine("[DataMember]");
				WriteLine("public Guid {0};",column.Name);
			}			
		}
		PopIndent();
		WriteLine("}");
		PopIndent();
		WriteLine("}");
        return this.GenerationEnvironment.ToString();
    }
}
#>

 

Generator

<#@ import namespace="System.Collections.Generic" #>
<#@ include file="ProcedureTemplate.tt" #>
<#@ include file="T4Toolbox.tt" #>
<#+
public class ProcedureGenerator : Generator
{
    public List<ProcedureTemplate> Templates{get; private set;}
  
  	public ProcedureGenerator(string databaseName, params string[] tableNames)
	{
		if(null == tableNames || tableNames.Length == 0)
		{
			throw new ArgumentNullException("数据表名有误");
		}	
		Templates = new List<ProcedureTemplate>();
		foreach(string tableName in tableNames)
		{			
			var t = new ProcedureTemplate(databaseName, tableName);
			Templates.Add(t);
		}
	}
	
	protected override void RunCore()
	{
		foreach(ProcedureTemplate tempalte in this.Templates)
		{
			tempalte.RenderToFile(tempalte.GetProcedureName() + ".cs");
		}
    }
}
#>
 

 

生成器

<#@ template language="C#" hostSpecific="true" debug="true" #>
<#@ include file="ProcedureGenerator.tt" #>
<#
   var tableNames = new string[]{"aa","bb"};
   new ProcedureGenerator("testDb",tableNames).Run();
#>
 

此文参考了artech的文章

http://www.cnblogs.com/artech/archive/2010/11/17/CodeGeneration.html

目录
相关文章
存储 人工智能 机器人
331 0
|
数据可视化 BI API
无缝对接云数据库:自定义报表生成工具在混合云环境下的部署指南
自定义报表生成工具通过拖拽设计、多数据源整合及自动化输出,帮助业务人员零代码创建个性化报表,解决传统工具灵活性不足、技术门槛高的问题。文章对比其与传统报表差异,列举行业应用场景(如财务、零售),并给出选型建议与主流工具(如FineReport、Power BI、板栗看板)的优劣势分析。
471 0
|
人工智能 数据挖掘 API
基于neo4j数据库和dify大模型框架的rag模型搭建——后续补充
基于neo4j数据库和dify大模型框架的rag模型搭建——后续补充
1213 21
基于neo4j数据库和dify大模型框架的rag模型搭建——后续补充
|
Java 数据库 Docker
基于neo4j数据库和dify大模型框架的rag模型搭建
基于neo4j数据库和dify大模型框架的rag模型搭建
4086 35
|
数据库 Python
django中数据库外键可以自定义名称吗
django中数据库外键可以自定义名称吗
|
前端开发 IDE 数据库连接
ThinkPHP6 模型层的模型属性,表映射关系,以及如何在控制层中使用模型层和模型层中的简单CRUD
本文详细介绍了ThinkPHP6中模型层的使用,包括模型属性设置、表映射关系、以及如何在控制层中使用模型层进行CRUD操作。
ThinkPHP6 模型层的模型属性,表映射关系,以及如何在控制层中使用模型层和模型层中的简单CRUD
|
前端开发 数据库 开发者
数据模型(数据库表设计)生成代码
BizWorks ToolKit 插件集成 Mybatis-Plus 代码生成工具,支持从数据库表批量生成代码,简化开发流程。本文详细介绍配置方法及项目示例,包括配置文件格式、生成选项及具体操作步骤,帮助开发者快速实现代码同步更新。配置文件 `.mp.yaml` 支持自定义输出目录、生成组件等,适用于多种项目结构。
292 1
|
11月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
11月前
|
SQL 关系型数据库 MySQL
Mysql数据恢复—Mysql数据库delete删除后数据恢复案例
本地服务器,操作系统为windows server。服务器上部署mysql单实例,innodb引擎,独立表空间。未进行数据库备份,未开启binlog。 人为误操作使用Delete命令删除数据时未添加where子句,导致全表数据被删除。删除后未对该表进行任何操作。需要恢复误删除的数据。 在本案例中的mysql数据库未进行备份,也未开启binlog日志,无法直接还原数据库。

热门文章

最新文章