MySQL生成C#实体类


调用MySQL存储过程来为数据表生成对应的C#实体类,可节省开发人员编码的时间,提高效率。

执行存储过程需要“数据库名称”和“表名称”两个参数:

CALL GenCSharpModel('数据库名称', '表名称');

执行结果如下图:

 

存储过程代码如下:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GenCSharpModel`(in pDataBaseName VARCHAR(255),in pTableName VARCHAR(255))
BEGIN
DECLARE vClassName varchar(255);
declare vClassCode mediumtext;
declare colComment varchar(1024);
declare colType varchar(1024);
declare fName varchar(1024);
declare fNameNew varchar(1024);

DECLARE v_finished INTEGER DEFAULT 0;

DEClARE code_cursor CURSOR FOR 
            SELECT ColumnComment,ColumnType,FieldName FROM temp1; 

DECLARE CONTINUE HANDLER FOR 
            NOT FOUND SET v_finished = 1;

            -- 生成实体类名
            SELECT REPLACE(GROUP_CONCAT(CONCAT(UPPER(LEFT(A.Field,1)),SUBSTRING(A.Field,2,(LENGTH(A.Field)-1)))),',','')
            
            FROM (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(pTableName,'_',help_topic_id+1),'_',-1) AS Field 
                        FROM mysql.help_topic 
            WHERE help_topic_id < LENGTH(pTableName)-LENGTH(REPLACE(pTableName,'_',''))+1) A 
                        
            into vClassName ;

      -- 创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS  temp1 ENGINE=MyISAM  
    as (
                    SELECT  COLUMN_NAME  AS FieldName, COLUMN_COMMENT AS ColumnComment,
                        case  
                            when DATA_TYPE='bigint' and IS_NULLABLE = 'YES'  then 'long?'
                            when DATA_TYPE='bit' and IS_NULLABLE = 'YES'  then 'bool?'
                            when DATA_TYPE='char' and COLUMN_TYPE = 'char(36)' and IS_NULLABLE = 'YES' then 'Guid?'
                            when DATA_TYPE='date' and IS_NULLABLE = 'YES'  then 'DateTime?'
                            when DATA_TYPE='datetime' and IS_NULLABLE = 'YES' then 'DateTime?'
                            when DATA_TYPE='decimal' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='float' and IS_NULLABLE = 'YES' then 'float?'
                            when DATA_TYPE='int' and IS_NULLABLE = 'YES' then 'int?'
                            when DATA_TYPE='numeric' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='decimal' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='double' and IS_NULLABLE = 'YES' then 'double?'
                            when DATA_TYPE='real' and IS_NULLABLE = 'YES' then 'double?'
                            when DATA_TYPE='smallint' and IS_NULLABLE = 'YES' then 'short?'
                            when DATA_TYPE='mediumint' and IS_NULLABLE = 'YES' then 'int?'
                            when DATA_TYPE='time'  and IS_NULLABLE = 'YES' then 'TimeSpan?'
                            when DATA_TYPE='timestamp' and IS_NULLABLE = 'YES' then 'DateTime?'
                            when DATA_TYPE='tinyint' and COLUMN_TYPE = 'tinyint(1)' and IS_NULLABLE = 'YES' then 'bool?'
                            when DATA_TYPE='tinyint' and IS_NULLABLE = 'YES' then 'byte?'
                            when DATA_TYPE='year'  and IS_NULLABLE = 'YES' THEN 'uint?'
                            
                            when DATA_TYPE='bigint' then 'long'
                            when DATA_TYPE='binary' then 'byte[]'
                            when DATA_TYPE='bit' then 'bool'                
                            when DATA_TYPE='char' and COLUMN_TYPE = 'char(36)' then 'Guid'                    
                            when DATA_TYPE='date' then 'DateTime'                
                            when DATA_TYPE='datetime' then 'DateTime'    
                            when DATA_TYPE='decimal' then 'decimal'    
                            when DATA_TYPE='float' then 'float'
                            when DATA_TYPE='image' then 'byte[]'
                            when DATA_TYPE='int' then 'int'
                            when DATA_TYPE='numeric' then 'decimal'
                            when DATA_TYPE='decimal' then 'decimal'
                            when DATA_TYPE='double' then 'double'
                            when DATA_TYPE='real' then 'double'
                            when DATA_TYPE='smallint' then 'short'
                            when DATA_TYPE='mediumint' then 'int'
                            when DATA_TYPE='text' then 'string'
                            when DATA_TYPE='mediumtext' then 'string'
                            when DATA_TYPE='longtext' then 'string'                        
                            when DATA_TYPE='time' then 'TimeSpan'                        
                            when DATA_TYPE='timestamp' then 'DateTime'                        
                            when DATA_TYPE='tinyint' and COLUMN_TYPE = 'tinyint(1)'  then 'bool'
                            when DATA_TYPE='tinyint' then 'byte'
                            when DATA_TYPE='varbinary' then 'byte[]'
                            when DATA_TYPE='varchar' then 'string'
                            when DATA_TYPE='year' THEN 'uint'
                            else 'UNKNOWN_' + DATA_TYPE
                        end AS ColumnType
                        FROM ( SELECT  COLUMN_NAME, DATA_TYPE, COLUMN_TYPE,COLUMN_COMMENT,IS_NULLABLE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE table_schema=pDataBaseName and table_name = pTableName) A);

    set vClassCode = '';
        
        -- 打开游标
    OPEN code_cursor;

            get_code: LOOP

                FETCH code_cursor INTO  colComment,colType,fName;

                IF v_finished = 1 THEN
                    LEAVE get_code;
                END IF;
                                
                                -- 处理字段
                                SELECT REPLACE(GROUP_CONCAT(CONCAT(UPPER(LEFT(A.Field,1)),SUBSTRING(A.Field,2,(LENGTH(A.Field)-1)))),',','')
                                FROM 
                                ( SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(fName,'_',help_topic_id+1),'_',-1) AS Field 
                                        FROM mysql.help_topic 
                                        WHERE help_topic_id < LENGTH(fName)-LENGTH(REPLACE(fName,'_',''))+1 ) A into fNameNew;

                -- 组合代码字符串
                select  CONCAT(vClassCode,'\r\n\r\n', concat( '/// <summary>\r\n','/// ',colComment,'\r\n/// </summary>\r\n', 'public ', colType , ' ' ,fNameNew,' { get; set; }')) 
                                into  vClassCode ;

            END LOOP get_code;

        CLOSE code_cursor;

        -- 删除临时表
        drop table temp1;
    
        -- 最终生成类
        select concat('public class ',vClassName,'\r\n{', vClassCode,'\r\n}') as ModelClass;
END

 

若表名、字段名的命名规则不同或有特殊生成规则,则需进行相应的修改。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM