返回顶部

CodeSmith无法获取MySQL表及列Description说明处理

实体类生成模板下载:

链接:https://pan.baidu.com/s/1tLxW5m5ECwVV2feWSVtQIA
提取码:qezw

反编译工具下载:

链接:https://pan.baidu.com/s/19dG4NweQodLl0yG5XQrcOg
提取码:r793

一、使用反编译工具dnSpy打开D:\software\generator71(1)\generator-71\SchemaProviders\SchemaExplorer.MySQLSchemaProvider.dll

二、找到MySQLSchemaProvider类

 

 

 三、找到GetExtendedProperties方法名,使用反编译工具编辑该类,替换GetExtendedProperties的代码逻辑并进行保存

public ExtendedProperty[] GetExtendedProperties(string connectionString, SchemaObjectBase schemaObject)
        {
            List<ExtendedProperty> list = new List<ExtendedProperty>();
            if (schemaObject is ColumnSchema)
            {
                ColumnSchema columnSchema = schemaObject as ColumnSchema;
                string commandText = string.Format("SELECT EXTRA, COLUMN_DEFAULT, COLUMN_TYPE, COLUMN_COMMENT\r\n                                                        FROM INFORMATION_SCHEMA.COLUMNS\r\n                                                        WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}' AND COLUMN_NAME = '{2}'", columnSchema.Table.Database.Name, columnSchema.Table.Name, columnSchema.Name);
                using (DbConnection dbConnection = MySQLSchemaProvider.CreateConnection(connectionString))
                {
                    dbConnection.Open();
                    DbCommand dbCommand = dbConnection.CreateCommand();
                    dbCommand.CommandText = commandText;
                    dbCommand.Connection = dbConnection;
                    using (IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dataReader.Read())
                        {
                            string text = dataReader.GetString(0).ToLower();
                            bool flag = dataReader.IsDBNull(1);
                            string value = "";
                            if (!flag)
                            {
                                value = dataReader.GetString(1).ToUpper();
                            }
                            string value2 = dataReader.GetString(2).ToUpper();
                            string @string = dataReader.GetString(3);
                            bool flag2 = text.IndexOf("auto_increment") > -1;
                            list.Add(new ExtendedProperty("CS_IsIdentity", flag2, columnSchema.DataType));
                            if (flag2)
                            {
                                list.Add(new ExtendedProperty("CS_IdentitySeed", 1, columnSchema.DataType));
                                list.Add(new ExtendedProperty("CS_IdentityIncrement", 1, columnSchema.DataType));
                            }
                            list.Add(new ExtendedProperty("CS_ColumnDefaultIsNull", flag, DbType.Boolean));
                            list.Add(new ExtendedProperty("CS_Default", value, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnDefault", value, DbType.String));
                            list.Add(new ExtendedProperty("CS_SystemType", value2, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnType", value2, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnExtra", text.ToUpper(), DbType.String));
                            list.Add(new ExtendedProperty("CS_Description", @string, DbType.String));
                        }
                        if (!dataReader.IsClosed)
                        {
                            dataReader.Close();
                        }
                    }
                    if (dbConnection.State != ConnectionState.Closed)
                    {
                        dbConnection.Close();
                    }
                }
            }
            if (schemaObject is TableSchema)
            {
                TableSchema tableSchema = schemaObject as TableSchema;
                string commandText2 = string.Format("SHOW CREATE TABLE `{0}`.`{1}`", tableSchema.Database.Name, tableSchema.Name);
                using (DbConnection dbConnection2 = MySQLSchemaProvider.CreateConnection(connectionString))
                {
                    dbConnection2.Open();
                    DbCommand dbCommand2 = dbConnection2.CreateCommand();
                    dbCommand2.CommandText = commandText2;
                    dbCommand2.Connection = dbConnection2;
                    using (IDataReader dataReader2 = dbCommand2.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dataReader2.Read())
                        {
                            string string2 = dataReader2.GetString(1);
                            list.Add(new ExtendedProperty("TS_Description", string2, DbType.String));
                            int num = string2.LastIndexOf("ENGINE");
                            int num2 = string2.LastIndexOf("COMMENT=");
                            string value3 = dataReader2.GetString(0);
                            if (num2 > num)
                            {
                                value3 = string2.Substring(num2 + 9).Replace("'", "");
                            }
                            list.Add(new ExtendedProperty("CS_Description", value3, DbType.String));
                        }
                        if (!dataReader2.IsClosed)
                        {
                            dataReader2.Close();
                        }
                    }
                    if (dbConnection2.State != ConnectionState.Closed)
                    {
                        dbConnection2.Close();
                    }
                }
            }
            return list.ToArray();
        }

四、生成效果

 

posted @ 2022-09-24 14:36  SportSky  阅读(133)  评论(0编辑  收藏  举报
作者:SportSky 出处: http://www.cnblogs.com/sportsky/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。