Custom tt Text Template implementation MySql refers to the entity class generated in the database, ttmysql

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.