Custom tt Text Template implementation MySql refers to the entity class generated in the database, ttmysql
1. Click "add"/"new item" in the project, select "Text Template", enter a name, and click "add.
2. Add the following code to Base. tt.
<# @ Template debug = "false" hostspecific = "false" language = "C #" #>< # @ output extension = ". cs "#> <# @ Assembly Name =" System. core "#> <# @ Assembly Name =" System. windows. forms "#> <# @ Assembly Name =" MySql. data. dll "#> <# @ Assembly Name =" System. data "#> <# @ Assembly Name =" System "#> <# @ import namespace =" System "#> <# @ import namespace =" System. IO "#> <# @ import namespace =" System. diagnostics "#> <# @ import namespace = "System. linq "#> <# @ import namespace =" System. collections "#> <# @ import namespace =" System. collections. generic "#> <# @ import namespace =" MySql. data "#> <# @ import namespace =" MySql. data. mySqlClient "#> // set basic database information <# string Server =" localhost "; string UId =" root "; string PWd =" fxy19940923 .. "; string Db_Name =" cxkdb "; #>// obtain basic data of each table in the database <# string conStr =" server = "+ Server + "; database = "+ Db_Name +"; uid = "+ UId +"; pwd = "+ PWd; MySqlConnection con = new MySqlConnection (conStr); con. open (); MySqlParameter mp = new MySqlParameter ("@ db_name", Db_Name); string sqlStr = "select * from information_schema.COLUMNS where table_schema = @ db_name "; mySqlCommand cmd = new MySqlCommand (sqlStr, con); cmd. parameters. add (mp); MySqlDataReader dr = cmd. executeReader (); var TableList = new List <string> (); var DetailList = new List <Tuple <string, strin G, string >>(); var TableName = ""; while (dr. read () {var Table = dr ["TABLE_NAME"]. toString (); if (TableName = Table) {var Column = dr ["COLUMN_NAME"]. toString (); var DataTypeOld = dr ["DATA_TYPE"]. toString (); var DataType = DataTypeOld = "varchar "? "String": (DataTypeOld = "int "? "Int": (DataTypeOld = "datetime "? "DateTime": (DataTypeOld = "decimal "? "Decimal": (DataTypeOld = "char "? "String": (DataTypeOld = "bit "? "Boolean": "string"); DetailList. add (Tuple. create (Table, DataType, Column);} else {TableList. add (Table) ;}tablename = Table ;#> using System; using System. collections. generic; using System. linq; using System. text; using System. data;
3. Create a DataBaseEntity. tt Text Template Using the Method in step 1 and add the following code.
<#@ template debug="false" hostspecific="false" language="C#" #><#@ output extension=".cs" #><#@ include file="Base.tt" #>namespace DataBaseEntity{<# foreach(var item in TableList) { #> public class <#= item #> {<# foreach(var detail in DetailList) {if( item == detail.Item1 ) { #> public <#=detail.Item2#> <#=detail.Item3#> { get; set; } <#}}#> }<# } #>}
4. fill in the basic information of the database in the Base. in the tt file, that is, the "Basic settings of the Database" section, and then save the Base in sequence. tt, DataBaseEntity. tt, in the DataBase. the corresponding entity class is generated in the tt directory.