码迷,mamicode.com
首页 > 其他好文 > 详细

实体生成

时间:2017-08-26 19:55:39      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:toc   his   tuple   sem   deb   oat   imp   tab   log   

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ Assembly Name="$(ProjectDir)\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" #>

<#


    string Db_Name="repairsystem",Table_Name="stock_infolist";
    string conStr="server=localhost;database=repairsystem;uid=root;pwd=root";
    MySqlConnection con=new MySqlConnection(conStr);
    con.Open();
    MySqlParameter mp1=new MySqlParameter("@db_name",Db_Name);
    MySqlParameter mp2=new MySqlParameter("@table_name",Table_Name);
    string sqlStr="select * from information_schema.COLUMNS where table_schema=@db_name ";//and table_name=@table_name";
    MySqlCommand cmd=new MySqlCommand(sqlStr,con);
    cmd.Parameters.Add(mp1);
    cmd.Parameters.Add(mp2);
    MySqlDataReader dr = cmd.ExecuteReader();

#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@  include file="Base.tt" #>
<#
    var TableList=new List<string>();
    var DetailList=new List<Tuple<string,string,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;
    }
#>
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; } 
<#}}#>    }
<# } #>}
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="Base.tt" #> 

<#
    var TableInfo="";
    List<Tuple<string,string>> list=new List<Tuple<string,string>>();
    while(dr.Read())
    {
        var TableName=dr["table_name"].ToString();
        if(TableName==TableInfo)
        {
            var Column=dr["column_name"].ToString();
            var DataType=dr["data_type"].ToString();
            switch(DataType)
            {
                case "varchar":DataType="string";
                    break;
                case "int":DataType="int";
                    break;
                case "datetime":DataType="DateTime";
                    break;
                case "decimal":DataType="decimal";
                    break;
                case "char":DataType="string";
                    break;
                case "double":DataType="double";
                    break;
                case "float":DataType="float";
                    break;
                case "bit":DataType="Boolean";
                    break;
                default:DataType="string";
                    break;
            }
            list.Add(Tuple.Create(Column,DataType));
        }
        TableInfo=TableName;
    }
#>
// This is the output code from your template
// you only get syntax-highlighting here - not intellisense

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data;

namespace MyNameSpace
{
    class <#= TableInfo #>
    {
    <#
    foreach(var item in list)
    {
    #>
    public <#= item.Item2 #> <#= item.Item1 #> { get ; set ; }
    <#
    }
    #>
}
}
 

 

实体生成

标签:toc   his   tuple   sem   deb   oat   imp   tab   log   

原文地址:http://www.cnblogs.com/fuxuyang/p/7435927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!