How to generate model from database using Dapper?

前端 未结 8 1365
故里飘歌
故里飘歌 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:44

    Calling the stored procedure from a cursor

    If you combine the sp mattritchies mentioned (see answer above) and call it from a cursor you can generate the poco class for every table in your database

    USE YourDataBaseName
    GO 
        DECLARE @field1 nvarchar(400)
        DECLARE cur CURSOR LOCAL for
    
        SELECT TABLE_NAME FROM information_schema.tables
        OPEN cur
        FETCH NEXT FROM cur INTO @field1 --, @field2
        WHILE @@FETCH_STATUS = 0 BEGIN          
            exec Helper_CreatePocoFromTableName @field1 -- , @field2            
            fetch next from cur into @field1 -- , @field2
        END
    
    close cur
    deallocate cur
    

    Stored Procedure mattritchies mentioned

    I took the sql from mattritchies answer (see above) and created the stored procedure he mentioned and modified it a bit so that it adds the class name as well. If you put Management Studio into Text-Output-Mode and remove the output of the column names you get copy paste text for all classes:

    CREATE PROCEDURE [dbo].[Helper_CreatePocoFromTableName]    
        @tableName varchar(100)
    AS
    BEGIN
    SET NOCOUNT ON;
    
    -- Subquery to return only the copy paste text
    Select PropertyColumn from (
        SELECT 1 as rowNr, 'public class ' + @tableName + ' {' as PropertyColumn
        UNION
        SELECT 2 as rowNr, 'public ' + a1.NewType + ' ' + a1.COLUMN_NAME + ' {get;set;}' as PropertyColumn
        -- ,* comment added so that i get copy pasteable output
         FROM 
        (
            /*using top because i'm putting an order by ordinal_position on it. 
            putting a top on it is the only way for a subquery to be ordered*/
            SELECT TOP 100 PERCENT
            COLUMN_NAME,
            DATA_TYPE,
            IS_NULLABLE,
            CASE 
                WHEN DATA_TYPE = 'varchar' THEN 'string'
                WHEN DATA_TYPE = 'nvarchar' 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 = 'smalldatetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
                WHEN DATA_TYPE = 'datetime2' AND IS_NULLABLE = 'NO' THEN 'DateTime'
                WHEN DATA_TYPE = 'smalldatetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
                WHEN DATA_TYPE = 'datetime2' 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 = 'char' THEN 'string'                       
                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?'
                WHEN DATA_TYPE = 'xml' THEN 'string'
            END AS NewType
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME = @tableName
            ORDER BY ORDINAL_POSITION
            ) AS a1 
        UNION 
        SELECT 3 as rowNr,  '} // class ' + @tableName
        ) as t Order By rowNr asc
    END
    

    P.S.: I would have done it as an edit suggestion to his answers but my experience is that often edit suggestions get rejected.

    Update

    User chris-w-mclean suggested the following changes (see his suggested-edit) which i have not tried myself:

    • Replace SELECT 1 as rowNr, 'public class ' with SELECT 1.0 as rowNr, 'public class '
    • Replace SELECT 2 as rowNr, 'public ' with SELECT 2 + a1.ORDINAL_POSITION/1000 as rowNr, 'public '
    • Replace SELECT TOP 100 PERCENT COLUMN_NAME, with SELECT COLUMN_NAME,
    • add between IS_NULLABLE, CASE this line cast(ORDINAL_POSITION as float) as ORDINAL_POSITION,
    • remove ORDER BY ORDINAL_POSITION
    • change SELECT 3 as to SELECT 3.0 as

提交回复
热议问题