时间:2021-07-01 10:21:17 帮助过:8人阅读
CodeSmith虽然方便,但是要安装和激活,这个是很麻烦的;而且每次生成都要打开CodeSmith去生成,不是很方便;
于是我就照着原先在CodeSmith上模板写了个控制台应用程序,可以改写配合着bat使用,贼方便
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Text; 6 using System.Linq; 7 using System.IO; 8 9 namespace Blog.Core.Test 10 { 11 public class Program 12 { 13 /// <summary> 14 /// 数据库连接字符串 15 /// </summary> 16 private static string _connstr = "Data Source=localhost;Initial Catalog=Test;User Id=sa;Password=123456"; 17 18 /// <summary> 19 /// 主函数 20 /// </summary> 21 /// <param name="args"></param> 22 static void Main(string[] args) 23 { 24 Console.Write("命名空间:"); 25 string namespaces = Console.ReadLine(); 26 Console.Write("文件名:"); 27 string filename = Console.ReadLine(); 28 Console.WriteLine("开始生成,请等待..."); 29 new Program().Generate(namespaces, filename); 30 Console.WriteLine("生成成功..."); 31 Console.ReadKey(); 32 } 33 34 /// <summary> 35 /// 生成Model文件 36 /// </summary> 37 /// <param name="namespaces"></param> 38 /// <param name="filename"></param> 39 private void Generate(string namespaces, string filename) 40 { 41 byte[] myByte = Encoding.UTF8.GetBytes(BuildTemplete(namespaces)); 42 string filepath = Environment.CurrentDirectory + "\\" + filename; 43 if (File.Exists(filepath)) 44 { 45 File.Delete(filepath); 46 } 47 using (FileStream fsWrite = new FileStream(filepath, FileMode.Append)) 48 { 49 fsWrite.Write(myByte, 0, myByte.Length); 50 }; 51 } 52 53 /// <summary> 54 /// 创建模板 55 /// </summary> 56 /// <param name="namespaces"></param> 57 /// <returns></returns> 58 private string BuildTemplete(string namespaces) 59 { 60 StringBuilder templete = new StringBuilder("using System;"); 61 templete.Append("using System.Collections.Generic;\n\n"); 62 templete.AppendFormat("namespace {0}\n{{\n", namespaces); 63 List<TableModel> tables = GetTables(); 64 foreach (var table in tables) 65 { 66 templete.AppendFormat(" #region {0}\n", table.name); 67 templete.Append(" /// <summary>\n"); 68 templete.AppendFormat(" /// {0}模型\n", table.name); 69 templete.Append(" /// </summary>\n"); 70 templete.Append(" [Serializable]\n"); 71 templete.AppendFormat(" public class {0} : BaseModel\n {{", table.name); 72 templete.Append("\n"); 73 templete.Append(" /// <summary>\n"); 74 templete.Append(" /// 表名\n"); 75 templete.Append(" /// </summary>\n"); 76 templete.AppendFormat(" public static readonly string TableName = \"{0}\";\n", table.name); 77 templete.Append("\n"); 78 templete.Append(" /// <summary>\n"); 79 templete.Append(" /// 构造函数\n"); 80 templete.Append(" /// </summary>\n"); 81 templete.AppendFormat(" public {0}() : base(TableName) {{ }}\n", table.name); 82 templete.Append(" private Guid Id = Guid.Empty;\n"); 83 table.columns.ForEach(columu => 84 { 85 templete.Append("\n"); 86 templete.Append(" /// <summary>\n"); 87 templete.AppendFormat(" /// {0}\n", columu.ColComment); 88 templete.Append(" /// </summary>\n"); 89 if (columu.IsPk) 90 { 91 templete.AppendFormat(" public Guid {0}\n", columu.ColName); 92 templete.Append(" {\n"); 93 templete.Append(" get { return Id; }\n"); 94 templete.Append(" set\n"); 95 templete.Append(" {\n"); 96 templete.Append(" Id = value;\n"); 97 templete.Append(" if (value != null)\n"); 98 templete.Append(" {\n"); 99 templete.Append(" base.BaseId = value;\n"); 100 templete.Append(" }\n"); 101 templete.Append(" }\n"); 102 templete.Append(" }\n"); 103 } 104 else 105 { 106 templete.AppendFormat(" public {0} {1} {{ get; set; }} {2}\n", GetCSType(columu.ColType), columu.ColName, GetCSDefault(columu.ColDefault)); 107 } 108 }); 109 templete.Append(" }"); 110 111 templete.Append("\n"); 112 113 templete.Append(" /// <summary>\n"); 114 templete.AppendFormat(" /// {0}数据模型\n", table.name); 115 templete.Append(" /// </summary>\n"); 116 templete.Append(" [Serializable]\n"); 117 templete.AppendFormat(" public class {0}ListData\n {{", table.name); 118 templete.Append("\n"); 119 templete.Append(" /// <summary>\n"); 120 templete.Append(" /// 总记录数\n"); 121 templete.Append(" /// </summary>\n"); 122 templete.Append(" public int RecordCount { get; set; }\n"); 123 templete.Append(" /// <summary>\n"); 124 templete.Append("\n"); 125 templete.Append(" /// 数据列表\n"); 126 templete.Append(" /// </summary>\n"); 127 templete.AppendFormat(" public List<{0}ListModel> RecordList {{ get; set; }}\n", table.name); 128 templete.Append(" }"); 129 130 templete.Append("\n"); 131 132 templete.Append(" /// <summary>\n"); 133 templete.AppendFormat(" /// {0}列表模型\n", table.name); 134 templete.Append(" /// </summary>\n"); 135 templete.Append(" [Serializable]\n"); 136 templete.AppendFormat(" public class {0}ListModel\n {{", table.name); 137 templete.Append("\n"); 138 table.columns.ForEach(columu => 139 { 140 if (columu.ColName != "IsDeleted") 141 { 142 templete.Append("\n"); 143 templete.Append(" /// <summary>\n"); 144 templete.AppendFormat(" /// {0}\n", columu.ColComment); 145 templete.Append(" /// </summary>\n"); 146 if (new string[] { "Guid", "DateTime" }.Contains(GetCSType(columu.ColType))) 147 { 148 templete.AppendFormat(" public string {0} {{ get; set; }}\n", columu.ColName); 149 } 150 else 151 { 152 templete.AppendFormat(" public {0} {1} {{ get; set; }}\n", GetCSType(columu.ColType), columu.ColName); 153 } 154 } 155 }); 156 templete.Append(" }\n"); 157 templete.Append(" #endregion\n"); 158 templete.Append("\n"); 159 } 160 templete = templete.Remove(templete.Length - 2, 1); 161 templete.Append("}"); 162 return templete.ToString(); 163 } 164 165 /// <summary> 166 /// 获取表数据 167 /// </summary> 168 /// <returns></returns> 169 private List<TableModel> GetTables() 170 { 171 List<TableModel> tables = new List<TableModel>(); 172 DataTable tabName = Query("SELECT name AS TableName FROM sysobjects WHERE xtype = ‘U‘"); 173 DataTable colName = Query(@"--获取表名、字段名称、字段类型、字段说明、字段默认值 174 SELECT obj.name AS TableName,--表名 175 col.name