PetaPoco的T4模板优化改进

主要修改Database.tt这个文件

优化点:

1.修改默认参数;

2.修改实体类名成为首字母大写的驼峰式写法,自动去掉下划线;

3.修改字段为首字母大写的驼峰式写法,自动去掉下划线;

<#@ include file="PetaPoco.Core.ttinclude" #>
<#
    // Settings
    ConnectionStringName = "";          // Uses last connection string in config if not specified
    Namespace = "";
    RepoName = "";
    GenerateOperations = true;
    GeneratePocos = true;
    GenerateCommon = true;
    ClassPrefix = "";
    ClassSuffix = "";
    TrackModifiedColumns = false;
    ExplicitColumns = true;
    ExcludePrefix = new string[] {}; // Exclude tables by prefix.

    // Read schema
    var tables = LoadTables();


/*
    // 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
*/
    foreach(Table tbl in from t in tables select t)  
    {  

            //处理表名称  
            string srcStr = tbl.Name.ToLower();  
            string newString = "";  
            int first = 0;  
            while (srcStr.IndexOf("_") != -1)  
            {  
                first = srcStr.IndexOf("_");  
                if (first != srcStr.Length)  
                {  
                    newString = newString + srcStr.Substring(0, first);  
                    srcStr = srcStr.Substring(first + 1);  
                    srcStr = srcStr.Substring(0, 1).ToUpper() + srcStr.Substring(1);  
                }  
            }  
            newString = newString + srcStr;  

            tables[tbl.Name].ClassName =ClassPrefix+newString.Substring(0, 1).ToUpper() + newString.Substring(1)+ClassSuffix;
            //处理字段名称
            foreach(Column col in from c in tbl.Columns select c)
            {
                if(col.Name.StartsWith("_"))
                {
                    tables[tbl.Name][col.Name].Ignore=true; 
                }
                string srcStrCol = col.Name.ToLower();
                string newStringCol = "";
                int firstCol = 0;
                while (srcStrCol.IndexOf("_") != -1)
                {
                    firstCol = srcStrCol.IndexOf("_");
                    if (firstCol != srcStrCol.Length)
                    {
                        newStringCol = newStringCol + srcStrCol.Substring(0, firstCol);
                        srcStrCol = srcStrCol.Substring(firstCol + 1);
                        srcStrCol = srcStrCol.Substring(0, 1).ToUpper() + srcStrCol.Substring(1);
                    }
                }
                newStringCol = newStringCol + srcStrCol;
                tables[tbl.Name][col.Name].PropertyName=newStringCol.Substring(0, 1).ToUpper() + newStringCol.Substring(1); 
            }
    }
    // Generate output
    if (tables.Count>0)
    {
#>
<#@ include file="PetaPoco.Generator.ttinclude" #>
<# } #>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值