diff --git a/DatabasetoEntityclass/Form1.cs b/DatabasetoEntityclass/Form1.cs index a60fff7..38e7114 100644 --- a/DatabasetoEntityclass/Form1.cs +++ b/DatabasetoEntityclass/Form1.cs @@ -78,24 +78,25 @@ namespace WindowsFormsApp1 var list = new List(); txt_ret.Text += "using System;\n" + "using NCA_MES_Models.CommonUtils.DB.DBAttribute;\n\n"; - txt_ret.Text += "namespace "+ namespaceName + "{\n\n"; + txt_ret.Text += $"namespace {namespaceName} {{\n\n"; txt_ret.Text += "/// " + "\n" + - "/// " + GetTableComment(tbname) + "\n"+ + $"/// {GetTableComment(tbname)}\n" + "/// " + "\n" + - "[TableName(\"" + tbname + "\")]" + "\n"+ - "public class " + GenerateClassName(tbname) + " {"; + $"[TableName(\"{tbname}\")]\n" + + $"public class {GenerateClassName(tbname)}: DBBaseModel<{GenerateClassName(tbname)}>{{"; for (int i = 0; i < dt.Rows.Count; i++) { var Name = dt.Rows[i]["Name"].ToString(); var Type = dt.Rows[i]["Type"].ToString(); var IsNullable = dt.Rows[i]["COMMENT"].ToString(); + var KeyType = dt.Rows[i]["KeyType"].ToString(); Type type = DbTypeStr_To_CsharpType(Type); - //txt_ret.Text += @"/// " + "\n" + "///" + IsNullable + "\n" + " /// \n" +""+ " public " + type.Name +" "+ Name + " { get; set; }" + "\n";//不加注释版本 - txt_ret.Text += "\n\t" + @"/// " + - "\n\t" + "///" + IsNullable + + txt_ret.Text += "\n\n\t" + "/// " + + "\n\t" + $"///{IsNullable}" + "\n\t" + "/// " + - "\n\t" + "[TableField(\""+ Name + "\")]" + - "\n\t"+ " public " + type.Name +" "+ ToCamelCase(Name) + " { get; set; }"; + (String.IsNullOrEmpty(KeyType)?"":"\n\t[TableId]") + + "\n\t" + $"[TableField(\"{Name}\",\"{IsNullable}\")]" + + "\n\t" + $"public {type.Name} {ToCamelCase(Name)} {{ get; set; }}"; //加注释记得引用using } txt_ret.Text += "\n}\n}"; @@ -301,9 +302,25 @@ namespace WindowsFormsApp1 /// public static DataTable GetDbTableInfo(string tabname) { - - string str = $"SELECT COLUMN_NAME AS Name, COLUMN_TYPE AS Type,COLUMN_COMMENT AS COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tabname}' ORDER BY ORDINAL_POSITION;"; - var dt = DbHelperMySQL.Query(str); + string sql = @"SELECT + c.COLUMN_NAME AS Name, + c.COLUMN_TYPE AS Type, + c.COLUMN_COMMENT AS Comment, + CASE WHEN CONSTRAINT_NAME = 'PRIMARY' THEN 'PRIMARY KEY' ELSE '' END AS KeyType + FROM + INFORMATION_SCHEMA.COLUMNS c + LEFT JOIN + INFORMATION_SCHEMA.KEY_COLUMN_USAGE k + ON + c.TABLE_SCHEMA = k.TABLE_SCHEMA + AND c.TABLE_NAME = k.TABLE_NAME + AND c.COLUMN_NAME = k.COLUMN_NAME + WHERE + c.TABLE_NAME = '{0}' + ORDER BY + c.ORDINAL_POSITION;"; + //string str = $"SELECT COLUMN_NAME AS Name, COLUMN_TYPE AS Type,COLUMN_COMMENT AS COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tabname}' ORDER BY ORDINAL_POSITION;"; + var dt = DbHelperMySQL.Query(String.Format(sql, tabname)); return dt; } @@ -351,6 +368,7 @@ namespace WindowsFormsApp1 { { "int", typeof(Int32) }, { "text", typeof(string) }, + { "enum", typeof(string) }, { "bigint", typeof(Int64) }, { "binary", typeof(byte[]) }, { "bit", typeof(bool) }, @@ -371,7 +389,7 @@ namespace WindowsFormsApp1 { "smallint", typeof(Int16) }, { "smallmoney", typeof(decimal) }, { "timestamp", typeof(DateTime) }, - { "tinyint", typeof(byte) }, + { "tinyint", typeof(string) }, { "varbinary", typeof(byte[]) }, { "varchar", typeof(string) }, { "variant", typeof(object) },