Model类代码生成器

时间:2023-03-09 00:16:46
Model类代码生成器
using Humanizer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CodeGenerate
{
class Program
{
static void Main(string[] args)
{
DataTable dt = ExecuteDataTable("select * from information_schema.tables");
foreach (DataRow row in dt.Rows)
{
string tablename = (string)row["TABLE_NAME"]; CreateModel(tablename);
} } private static string connectionString = "server=10.100.3.110,60005;User ID=sa;Password=sa123.com;database=p2p;Enlist=false";
private static string namespaceStr= "LLY.Model.LiCai";
private static string filePath = @"D:\Models";
static string kongge1 = "\t";
static string kongge2 = "\t\t";
static string kongge3 = "\t\t\t";
static string kongge4 = "\t\t\t\t";
StringBuilder SB = new StringBuilder(); #region 把数据库类型转化为.net类型
private static string ToNetType(string dataType)
{
switch (dataType)
{
case "bigint":
return "long?";
case "int":
return "int?";
case "nvarchar":
case "varchar":
case "char":
case "nchar":
return "string";
case "bit":
return "bool?";
case "datetime":
return "DateTime?";
default:
return "object";
}
}
#endregion #region 数据库连接操作
public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
//不能写成static
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
//WhetherCon(txtConnSr.Text);//待优化
/************此处写等待用户输入的代码********************/
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
return dt;
}
}
}
#endregion #region 生成Model
/// <summary>
/// 生成Model
/// </summary>
/// <param name="tablename"></param>
private static void CreateModel(string tablename)
{
string className = tablename.Singularize();
DataTable dtCols = ExecuteDataTable("select * from information_schema.columns where table_name=@tablename", new SqlParameter("tablename", tablename));//得到选中的表 DataTable dtColsDesc = ExecuteDataTable("SELECT major_id, minor_id, c.name as Column_Name, value AS extendedProperty "
+ "FROM sys.extended_properties AS ep "
+"INNER JOIN sys.tables AS t ON ep.major_id = t.object_id "
+"INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id "
+ "WHERE class = 1 and t.name =@tablename", new SqlParameter("tablename", tablename));//得到选中的表 StringBuilder sb = new StringBuilder();//用来拼接字符串的对象
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using LLY.Core.LiCai.Data;");
sb.AppendLine("using System.Threading.Tasks;\r\n");
sb.AppendLine("namespace " + namespaceStr);
sb.AppendLine("{\r\n\r\n");
sb.AppendLine(kongge1 + $"[Serializable]");
sb.AppendLine(kongge1 + $"public class {className} : BaseEntity");
sb.AppendLine(kongge1 + "{\r\n");
foreach (DataRow row in dtCols.Rows)
/*★参数中数据库类型和.net的数据类型之间的转换*/
//遍历每行,得到要用的参数,并赋给其它变量
{
string colName = (string)row["Column_Name"];
string dataType = (string)row["Data_Type"];
string netType = ToNetType(dataType); if(colName=="Id"|| colName == "CreatedAt" || colName == "LastModifiedAt")
{
continue;
} string propDesc = string.Empty;
foreach (DataRow item in dtColsDesc.Rows)
{
string colName1 = (string)item["Column_Name"];
string desc = (string)item["extendedProperty"];
if (colName1 == colName)
{
propDesc = desc;
break;
}
} sb.AppendLine(kongge2 + "/// <summary>");
sb.AppendLine(kongge2 + $"/// {propDesc}");
sb.AppendLine(kongge2 + "/// <summary>");
sb.AppendLine(kongge2 + "public" + " " + netType + " " + colName + "{ get; set; }\r\n"); }
sb.AppendLine(kongge1 + "}"); sb.AppendLine("\r\n\r\n");
sb.AppendLine(kongge1 + $"public class {className}Query : BaseQuery<{className}>");
sb.AppendLine(kongge1 + "{");
sb.AppendLine("\r\n\r\n");
sb.AppendLine(kongge1 + "}"); sb.AppendLine("}");
File.WriteAllText(filePath + @"\" + className + ".cs", sb.ToString());
}
#endregion } }