How to generate model from database using Dapper?

前端 未结 8 1351
故里飘歌
故里飘歌 2020-12-13 00:56

I am coming from PetaPoco camp. PetaPoco has a T4 template which generates model from the database. Is anything similar available for Dapper?

I installed Dapper usi

8条回答
  •  隐瞒了意图╮
    2020-12-13 01:36

    Try this version I optimized a bit, so that the result doesn't need to be piped to Text output. Instead, the PRINT statement allows the output to be copy/pasted easily. I've also removed the subquery and added declarations for nvarchar/ntext types.

    This is for a single table, but it can be converted to a stored proc to use one of the cursor suggestions above.

    SET NOCOUNT ON
    DECLARE @tbl as varchar(255)
    SET @tbl = '@@@@'
    
    DECLARE @flds as varchar(8000)
    SET @flds=''
    
    SELECT -1 as f0, 'public class ' + @tbl + ' {' as f1 into #tmp
    
    INSERT #tmp
    SELECT 
        ORDINAL_POSITION, 
        '    public ' + 
        CASE 
            WHEN DATA_TYPE = 'varchar' THEN 'string'
            WHEN DATA_TYPE = 'nvarchar' THEN 'string'
            WHEN DATA_TYPE = 'text' THEN 'string'
            WHEN DATA_TYPE = 'ntext' THEN 'string'
            WHEN DATA_TYPE = 'char' THEN 'string'
            WHEN DATA_TYPE = 'xml' THEN 'string'
            WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
            WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
            WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'YES' THEN 'int?'
            WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'NO' THEN 'int'
            WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'NO' THEN 'Int16'
            WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'YES' THEN 'Int16?'
            WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'NO' THEN 'long'
            WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'YES' THEN 'long?'
            WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'NO' THEN 'byte'
            WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'YES' THEN 'byte?'
            WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
            WHEN DATA_TYPE = 'varbinary' THEN 'byte[]'
            WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'NO' THEN 'bool'
            WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'YES' THEN 'bool?'
        END + ' ' + COLUMN_NAME + ' {get;set;}'
    FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME = @tbl
    
    INSERT #tmp SELECT 999, '}'
    
    SELECT @flds=@flds + f1 +'
    ' from #tmp order by f0
    
    DROP TABLE #tmp
    
    PRINT @flds
    

提交回复
热议问题