How to generate model from database using Dapper?

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

    This one is for Oracle. It's probably not complete, but it's worked for me thus far.

    SELECT 
    'public ' || A.NewType || ' ' || REPLACE(INITCAP(REPLACE(A.COLUMN_NAME, '_', ' ')), ' ', '') || ' {get;set;}' GET_SET
    , A.*
     FROM 
    (
    SELECT
    COLUMN_NAME,
    DATA_TYPE,
    NULLABLE,
    CASE 
        WHEN DATA_TYPE = 'VARCHAR2' THEN 'string'
        WHEN DATA_TYPE = 'VARCHAR' THEN 'string'
        WHEN DATA_TYPE = 'DATE' AND NULLABLE = 'N' THEN 'DateTime'
        WHEN DATA_TYPE = 'DATE' AND NULLABLE = 'Y' THEN 'DateTime?'
        WHEN DATA_TYPE = 'INT' AND NULLABLE = 'N' THEN 'int?'
        WHEN DATA_TYPE = 'INT' AND NULLABLE = 'Y' THEN 'int'
        WHEN DATA_TYPE = 'DECIMAL' AND NULLABLE = 'N' THEN 'decimal'
        WHEN DATA_TYPE = 'DECIMAL' AND NULLABLE = 'Y' THEN 'decimal?'
        WHEN DATA_TYPE = 'NUMBER' AND NULLABLE = 'N' THEN 'decimal'
        WHEN DATA_TYPE = 'NUMBER' AND NULLABLE = 'Y' THEN 'decimal?'
        WHEN DATA_TYPE = 'NUMBER2' AND NULLABLE = 'N' THEN 'decimal'
        WHEN DATA_TYPE = 'NUMBER2' AND NULLABLE = 'Y' THEN 'decimal?'
        WHEN DATA_TYPE = 'CHAR' THEN 'string'
        WHEN DATA_TYPE = 'CHAR2' THEN 'string'
        WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
        WHEN DATA_TYPE = 'CLOB' THEN 'byte[]'
        ELSE '??'
    END AS NewType
    FROM USER_TAB_COLUMNS
    WHERE TABLE_NAME = 'FIN_GLOBAL_CR_NUM_A'
    ORDER BY COLUMN_ID
    ) A
    
    0 讨论(0)
  • 2020-12-13 01:52

    I'm the author of a POCO-Generator template called CodegenCS.POCO.

    The link above contains C# and PowerShell Scripts which allow you to build complete POCOs (ready to use in Dapper) which also include override bool Equals(), override int GetHashCode(), and (for those who like it) it includes full ActiveRecord CRUD queries (Insert/Update).

    Check out this example POCO from Northwind database. If you like it, it's very easy to use the templates:

    • Edit the connection string and paths in RefreshSqlServerSchema.csx and invoke it through PowerShell script RefreshSqlServerSchema.ps1 This will Extract the Schema of a SQL Server database into a JSON file
    • Edit the paths and the POCOs Namespace in GenerateSimplePOCOs.csx and invoke it through PowerShell script GenerateSimplePOCOs.ps1 This will Read the JSON Schema and build the POCOs.

    The generator script is very simple to understand and customize.

    0 讨论(0)
提交回复
热议问题