T4 代码生成 Demo (抽奖程序)
参考自这位大狮的: https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt
项目Demo下载 http://download.csdn.net/detail/qq_21533697/9904071
- 支持Oracle,MSSQL,SQLite
- Demo项目是个抽奖小程序,抽奖只用到了LuckDraw表
- Demo用的SQLite包含库方便直接运行
- 里面用到Dapper就只写了Model层的模板,
文件目录
T4库
表结构读取抽象类 SchemaReader.ttinclude
- <#+
- /*
- The contents of this file are subject to the New BSD
- License (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of
- the License at http://www.opensource.org/licenses/bsd-license.php
- Software distributed under the License is distributed on an
- "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
- implied. See the License for the specific language governing
- rights and limitations under the License.
- */
- string ConnectionString = "";
- string TableFilter = "";
- string TopNamespace = "";
- string Namespace = "";
- string ClassPrefix = "";
- string ClassSuffix = "";
- string SchemaName = null;
- bool IncludeViews;
- string[] ExcludeTablePrefixes = new string[]{};
- string _connectionString="";
- string _providerName="";
- static Regex rxCleanUp = new Regex(@"[^\w\d_]", RegexOptions.Compiled);
- static Func<string, string> CleanUp = (str) =>
- {
- str = rxCleanUp.Replace(str, "_");
- if (char.IsDigit(str[])) str = "_" + str;
- return str;
- };
- string CheckNullable(Column col)
- {
- string result="";
- if(col.IsNullable &&
- col.PropertyType !="byte[]" &&
- col.PropertyType !="string" &&
- col.PropertyType !="Microsoft.SqlServer.Types.SqlGeography" &&
- col.PropertyType !="Microsoft.SqlServer.Types.SqlGeometry"
- )
- result="?";
- return result;
- }
- static bool IsExcluded(string tablename, string[] ExcludeTablePrefixes)
- {
- for (int i = ; i < ExcludeTablePrefixes.Length; i++)
- {
- string s = ExcludeTablePrefixes[i];
- if(tablename.StartsWith(s)) return true;
- }
- return false;
- }
- abstract class SchemaReader
- {
- public abstract Tables ReadSchema(string connstr, string tableFilter);
- public GeneratedTextTransformation outer;
- public void WriteLine(string o)
- {
- outer.WriteLine(o);
- }
- public string GetPropertyType(string sqlType)
- {
- string sysType = "string";
- switch (sqlType)
- {
- case "bigint":
- sysType = "long";
- break;
- case "smallint":
- sysType = "short";
- break;
- case "int":
- case "number":
- case "integer":
- sysType = "int";
- break;
- case "uniqueidentifier":
- sysType = "Guid";
- break;
- case "smalldatetime":
- case "datetime":
- case "date":
- case "time":
- sysType = "DateTime";
- break;
- case "float":
- sysType = "double";
- break;
- case "real":
- sysType = "float";
- break;
- case "numeric":
- case "smallmoney":
- case "decimal":
- case "money":
- sysType = "decimal";
- break;
- case "tinyint":
- sysType = "byte";
- break;
- case "bit":
- sysType = "bool";
- break;
- case "image":
- case "binary":
- case "varbinary":
- case "timestamp":
- sysType = "byte[]";
- break;
- case "geography":
- sysType = "Microsoft.SqlServer.Types.SqlGeography";
- break;
- case "geometry":
- sysType = "Microsoft.SqlServer.Types.SqlGeometry";
- break;
- }
- return sysType;
- }
- }
- public class Table
- {
- public List<Column> Columns;
- public string Name;
- public string Schema;
- public bool IsView;
- public string CleanName;
- public string ClassName;
- public string SequenceName;
- public bool Ignore;
- public Column PK
- {
- get
- {
- return this.Columns.SingleOrDefault(x=>x.IsPK);
- }
- }
- public Column GetColumn(string columnName)
- {
- return Columns.Single(x=>string.Compare(x.Name, columnName, true)==);
- }
- public Column this[string columnName]
- {
- get
- {
- return GetColumn(columnName);
- }
- }
- }
- public class Column
- {
- public string Name;
- public string PropertyName;
- public string PropertyType;
- public string DbType;
- public bool IsPK;
- public bool IsNullable;
- public bool IsAutoIncrement;
- public bool Ignore;
- }
- public class Tables : List<Table>
- {
- public Tables()
- {
- }
- public Table GetTable(string tableName)
- {
- return this.Single(x=>string.Compare(x.Name, tableName, true)==);
- }
- public Table this[string tableName]
- {
- get
- {
- return GetTable(tableName);
- }
- }
- }
- #>
SQLite表结构读取实现 SQLiteSchemaReader.ttinclude
- <#@ include file=".\SchemaReader.ttinclude" #>
- <#+
- Tables LoadTables()
- {
- WriteLine("// This file was automatically generated by the Dapper.SimpleCRUD T4 Template");
- WriteLine("// Do not make changes directly to this file - edit the template instead");
- WriteLine("// ");
- WriteLine("// The following connection settings were used to generate this file");
- WriteLine("// ");
- WriteLine("// Connection String : `{0}`", ConnectionString);
- WriteLine("");
- //DbProviderFactory _factory ;
- try
- {
- // _factory = DbProviderFactories.GetFactory(ProviderName);
- }
- catch (Exception x)
- {
- var error=x.Message.Replace("\r\n", "\n").Replace("\n", " ");
- WriteLine("");
- WriteLine("// -----------------------------------------------------------------------------------------");
- WriteLine("// -----------------------------------------------------------------------------------------");
- WriteLine("");
- return new Tables();
- }
- try
- {
- Tables result;
- SchemaReader reader= new SqliteSchemaReader();
- result=reader.ReadSchema(ConnectionString, TableFilter);
- for (int i=result.Count-; i>=; i--)
- {
- if (SchemaName!=null && string.Compare(result[i].Schema, SchemaName, true)!=)
- {
- result.RemoveAt(i);
- continue;
- }
- if (!IncludeViews && result[i].IsView)
- {
- result.RemoveAt(i);
- continue;
- }
- }
- var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$");
- foreach (var t in result)
- {
- t.ClassName = ClassPrefix + t.ClassName + ClassSuffix;
- foreach (var c in t.Columns)
- {
- c.PropertyName = rxClean.Replace(c.PropertyName, "_$1");
- // Make sure property name doesn't clash with class name
- if (c.PropertyName == t.ClassName)
- c.PropertyName = "_" + c.PropertyName;
- }
- }
- return result;
- }
- catch (Exception x)
- {
- var error=x.Message.Replace("\r\n", "\n").Replace("\n", " ");
- Warning(string.Format("Failed to read database schema - {0}", error));
- WriteLine("");
- WriteLine("// -----------------------------------------------------------------------------------------");
- WriteLine("// Failed to read database schema - {0}", error);
- WriteLine("// -----------------------------------------------------------------------------------------");
- WriteLine("");
- return new Tables();
- }
- }
- class SqliteSchemaReader : SchemaReader
- {
- private string _connstr {get;set;}
- public override Tables ReadSchema(string connstr, string tableFilter)
- {
- _connstr = connstr;
- var result = new Tables();
- //pull the tables in a reader
- using (IDataReader rdr = ExecuteReader(TABLE_SQL + tableFilter)) // SQLitehelper.
- {
- while (rdr.Read())
- {
- Table tbl = new Table();
- tbl.Name = rdr["name"].ToString();
- //tbl.Schema = rdr["TABLE_SCHEMA"].ToString();
- //tbl.IsView = string.Compare(rdr["TABLE_TYPE"].ToString(), "View", true) == 0;
- tbl.CleanName = CleanUp(tbl.Name);
- // tbl.CleanName = T4Generator.CleanUp(tbl.Name);
- if (tbl.CleanName.StartsWith("tbl_")) tbl.CleanName = tbl.CleanName.Replace("tbl_", "");
- if (tbl.CleanName.StartsWith("tbl")) tbl.CleanName = tbl.CleanName.Replace("tbl", "");
- tbl.CleanName = tbl.CleanName.Replace("_", "");
- tbl.ClassName = tbl.CleanName;
- result.Add(tbl);
- }
- }
- foreach (var tbl in result)
- {
- tbl.Columns = LoadColumns(tbl);
- //Mark the primary key
- //string PrimaryKey = GetPK(tbl.Schema, tbl.Name);
- //var pkColumn = tbl.Columns.SingleOrDefault(x => x.Name.ToLower().Trim() == PrimaryKey.ToLower().Trim());
- //if (pkColumn != null)
- //{
- // pkColumn.IsPK = true;
- //}
- }
- return result;
- }
- List<Column> LoadColumns(Table tbl)
- {
- var result = new List<Column>();
- using (IDataReader rdr = ExecuteReader(COLUMN_SQL.Replace("@tableName", tbl.Name))) // SQLitehelper.
- {
- while (rdr.Read())
- {
- Column col = new Column();
- col.Name = rdr["name"].ToString();
- col.PropertyName = CleanUp(col.Name);
- //col.PropertyName = T4Generator.CleanUp(col.Name);
- col.PropertyType = base.GetPropertyType(rdr["type"].ToString().ToLower());
- col.IsNullable = rdr["notnull"].ToString() != "";
- //col.IsAutoIncrement = false; //((int)rdr["IsIdentity"]) == 1;
- col.IsPK = rdr["pk"].ToString() == "";
- result.Add(col);
- }
- }
- return result;
- }
- string Table_Filter = " ";
- const string TABLE_SQL = " select name from sqlite_master where type = 'table' ";
- const string COLUMN_SQL = " PRAGMA table_info(@tableName) ";
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="sql">sql语句</param>
- /// <param name="slPars">参数</param>
- /// <returns>发挥SQLiteDataReader</returns>
- public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] slPars)
- {
- SQLiteConnection conn = new SQLiteConnection(_connstr);
- using (SQLiteCommand cmd = new SQLiteCommand(sql,conn))
- {
- if (slPars != null)
- {
- cmd.Parameters.AddRange(slPars);
- }
- try
- {
- conn.Open();
- return cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
- catch(Exception ex)
- {
- conn.Close();
- conn.Dispose();
- throw ex;
- }
- }
- }
- }
- #>
SQLite配置文件 SQLiteInit.ttinclude
1.配置连接串 注意修改连接串为自己对应的 ConnectionString = @"Data Source=E:/cc/test/LotterySite/Lib/db/cater.db;";
2.需要生成的表 3.顶级命名空间 4.一个库对应一个配置文件 (开始是把这些配置直接写在各层模板文件的,后来涉及多库的切换,每个文件改链接麻烦)
- <#@ include file="./SQLiteSchemaReader.ttinclude" #>
- <# // 初始化文件 一个库对应一个ttinclude文件
- // Settings 初始化配置
- ConnectionString = @"Data Source=E:/cc/test/LotterySite/Lib/db/cater.db;"; // 连接串
- TableFilter = " and name in ('LuckDraw') "; // 过滤表
- TopNamespace = "FW"; // 顶级命名空间
- ClassPrefix = "";
- ClassSuffix = "";
- IncludeViews = true;
- ExcludeTablePrefixes = new string[]{"aspnet_","webpages_"};
- // Read schema
- var tables = LoadTables(); //读取的所有表结构
- #>
SQLite 模板使用
- <#@ template hostspecific="True" #>
- <#@ include file="EF.Utility.CS.ttinclude"#>
- <#@ include file="$(SolutionDir)\FW.Common\T4Ttinclude\SQLiteInit.ttinclude" #>
- <#@ assembly name="EnvDTE" #>
- <#@ assembly name="System.Data" #>
- <#@ assembly name="System.Data.Entity.Design" #>
- <#@ assembly name="System.Xml" #>
- <#@ assembly name="System.Configuration" #>
- <#@ assembly name="$(SolutionDir)\Lib\sqlite\System.Data.SQLite.dll" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#@ import namespace="System.Data" #>
- <#@ import namespace="System.Data.Common" #>
- <#@ import namespace="System.Diagnostics" #>
- <#@ import namespace="System.Globalization" #>
- <#@ import namespace="System.IO" #>
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.Text" #>
- <#@ import namespace="System.Text.RegularExpressions" #>
- <#@ import namespace="System.Configuration" #>
- <#@ import namespace="System.Data.SQLite" #>
- <#@ output extension=".cst"#>
- <#
- Namespace = TopNamespace + ".TestModel";
- // Read schema
- EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); // 多文件生成
- /*
- // Tweak Schema
- tables["tablename"].Ignore = true; // To ignore a table
- tables["tablename"].ClassName = "newname"; // To change the class name of a table
- tables["tablename"]["columnname"].Ignore = true; // To ignore a column
- tables["tablename"]["columnname"].PropertyName="newname"; // To change the property name of a column
- tables["tablename"]["columnname"].PropertyType="bool"; // To change the property type of a column
- */
- #>
- <# fileManager.StartHeader(); #>
- using System;
- using Dapper;
- using Dapper.Contrib.Extensions;
- namespace <#=Namespace #>
- {
- <# fileManager.EndBlock(); #>
- <#
- foreach(Table tbl in from t in tables where !t.Ignore select t){
- if(IsExcluded(tbl.Name, ExcludeTablePrefixes)) continue;
- fileManager.StartNewFile(tbl.Name+".cs"); // 新建文件
- #>
- /// <summary>
- /// A class which represents the <#=tbl.Name#> <#=(tbl.IsView)?"view":"table"#>.
- /// </summary>
- [Table("[<#=tbl.Name#>]")]
- public partial class <#=tbl.ClassName#>
- {
- /* <#foreach(Column col in from c in tbl.Columns where !c.Ignore select c) {#> <#=col.PropertyName #> <#}#> */
- <#foreach(Column col in from c in tbl.Columns where !c.Ignore select c)
- {#>
- <# if (tbl.PK!=null && tbl.PK.Name==col.PropertyName) { #>
- [Key]
- <#}#>
- public virtual <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #> { get; set; }
- <#}#>
- }
- <#}#>
- <# fileManager.StartFooter(); #>
- } // namespace
- <# fileManager.EndBlock(); #>
- <# fileManager.Process(true); #>
模板属性:
Namespace 命名空间
tables 表结构集合
表属性
Name 数据库中的表名
ClassName 实体类名称
IsView 是否是视图(没怎么用)
PK.Name 主键列名
列属性
PropertyName 属性名称
PropertyType 属性类型
CheckNullable(col) 可空类型判断
注意引入对应的SQLite的T4配置库
<#@ include file="$(SolutionDir)\FW.Common\T4Ttinclude\SQLiteInit.ttinclude" #>
$(SolutionDir) 解决方案目录
T4 代码生成 Demo (抽奖程序)的更多相关文章
- sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)
sql的行转列(PIVOT)与列转行(UNPIVOT) 在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...
- 使用jQuery+PHP+Mysql实现抽奖程序
抽奖程序在实际生活中广泛运用,由于应用场景不同抽奖的方式也是多种多样的.本文将采用实例讲解如何利用jQuery+PHP+Mysql实现类似电视中常见的一个简单的抽奖程序. 查看演示 本例中的抽奖程序要 ...
- jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现
jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现 在线实例 查看演示 完整代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 一个好玩的jq+php实现转盘抽奖程序
前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...
- 幸运大转盘-jQuery+PHP实现的抽奖程序
目前好多网站上应用的转盘抽奖程序大多是基于flash的,而本文结合实例将使用jQuery和PHP来实现转盘抽奖程序,为了便于理解,作者分两部分来讲解,本文讲解第一部分,侧重使用jQuery实现转盘的转 ...
- java模拟一个抽奖程序
今天用一个程序模拟一个从1-32之间,随机抽取7组号码的抽奖程序 * 需要使用Java的图形界面知识 * 窗口 JFrame * 面板 JPanel * 显示文本信息的标签 JLabel * 文 ...
- jQuery幸运大转盘_jQuery+PHP抽奖程序
http://www.thinkphp.cn/code/1153.html 网上转盘抽奖程序大多是flash完成的,而本文使用jQuery和PHP来实现转盘抽奖程序. 若是想看更多js特效.网站源码. ...
- .net+mssql制作抽奖程序思路及源码
近期一直在研究数据库,刚好有个项目要做抽奖程序,恩,拿来练练手吧. 抽奖程序: 思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽 ...
- 简单的javascript抽奖程序
<html> <head> <title>手机号码抽奖程序</title> <script> //声明一个数组装住号码,可根 ...
随机推荐
- [.NET跨平台]Jeuxs独立版本的便利与过程中的一些坑
本文环境与前言 之前写过一篇相关的文章:在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验 当时的部署还是比较繁琐的,而且需要联网下载各种东西..有兴趣的可以看看, ...
- 01--数据库MySQL:【数据库DB】和【数据库管理系统DBMS】 简介
1.数据库DB 数据库:DB(DataBase) 按照一定规则存储在计算机的内部存储设备上被各种用户或者应用共享的数据集合 2.数据库管理系统DBMS 1)数据库管理系统DBMS:DBMS(DataB ...
- 一天搞定CSS:表单(form)--20
1.表单标签 2.input标签属性与取值 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UT ...
- 可视化之AQICN
上一篇和大家分享了<可视化之Berkeley Earth>,这次看一看下面这个网站---aqicn.org.先做一个提示:文末有惊喜~ 该网站在中国有一定的权威性,PM2.5数据有一点敏感 ...
- Android Studio的两种模式及签名配置
我们使用Android Studio 运行我们的app,无非两种模式:debug和release模式. debug模式 debug模式使用一个默认的debug.keystore进行签名. 这个默认签名 ...
- 如何修改"DEDECMS 提示信息!"方法!
dedecms程序使用过程中,经常有一些跳转提示信息会出现“DEDECMS 提示信息!”这几个字样. 很多朋友都想对他进行修改,改为自己网站的提示信息,其实方法也是很简单的,方法如下: 用编辑器打开i ...
- JavaScript数组知识点
强类型语言数组特点:连续的,指定好长度, 还要规定好数据类型弱类型语言数组特点:不一定是连续的 可以不用指定长度 不限定数据类型(可以存储任意类型的数据)数组定义方式:1.var arr=new Ar ...
- PHP:win7 ASP.NET环境与PHP(WAMP)环境如何共存
经验地址:http://jingyan.baidu.com/article/495ba8410f794d38b30ede89.html 笔记本以前安装过asp.net,启用了Windows的IIS服务 ...
- c++,函数名不要和某个类名相同 (syntax error : missing ';' before identifier....)
直接上代码: // outside.h class Outside { private: class Inner { public: Inner(Outside& out) : out_(ou ...
- v9 调用模型中新增的字段
在模型中新增字段的时候,可以选择“是否为主表”. 若选是,则前台调用可直接通过字段名调用. 若选否,在前台调用是应在{pc:content}中添加 moreinfo="1",表示允 ...