How to use dapper (simple object mapper) for MySQL in C#

Costas

Administrator
Staff member
Anirudha gupta writes at http://geekswithblogs.net/anirugu/archive/2017/09/08/how-to-use-dapper-for-mysql-in-c.aspx :
Few days ago I look for a solution so I can just save my time writing CRUD. So I found a solution. It’s called Dapper.

For creating c# class from the database you can use this code
JavaScript:
--src - https://gist.github.com/anirugu/9fb82ce773c45578f42f7a6d899f3221
set @schema := 'schema_name';
set @table := 'table_name';
SET group_concat_max_len = 2048;
SELECT 
    concat('public class ', @table, '\n{\n', GROUP_CONCAT(a.property_ SEPARATOR '\n'), '\n}') class_
FROM 
    (select
        CONCAT(
        '\tpublic ',
        case 
            when DATA_TYPE = 'bigint' then 'long'
            when DATA_TYPE = 'BINARY' then 'byte[]'
            when DATA_TYPE = 'bit' then 'bool'
            when DATA_TYPE = 'char' then 'string'
            when DATA_TYPE = 'date' then 'DateTime'
            when DATA_TYPE = 'datetime' then 'DateTime'
            when DATA_TYPE = 'datetime2' then 'DateTime'
            when DATA_TYPE = 'datetimeoffset' then 'DateTimeOffset'
            when DATA_TYPE = 'decimal' then 'decimal'
            when DATA_TYPE = 'double' then 'double'
            when DATA_TYPE = 'float' then 'float'
            when DATA_TYPE = 'image' then 'byte[]'
            when DATA_TYPE = 'int' then 'int'
            when DATA_TYPE = 'money' then 'decimal'
            when DATA_TYPE = 'nchar' then 'char'
            when DATA_TYPE = 'ntext' then 'string'
            when DATA_TYPE = 'numeric' then 'decimal'
            when DATA_TYPE = 'nvarchar' then 'string'
            when DATA_TYPE = 'real' then 'double'
            when DATA_TYPE = 'smalldatetime' then 'DateTime'
            when DATA_TYPE = 'smallint' then 'short'
            when DATA_TYPE = 'smallmoney' then 'decimal'
            when DATA_TYPE = 'text' then 'string'
            when DATA_TYPE = 'time' then 'TimeSpan'
            when DATA_TYPE = 'timestamp' then 'DateTime'
            when DATA_TYPE = 'tinyint' then 'bool'
            when DATA_TYPE = 'uniqueidentifier' then 'Guid'
            when DATA_TYPE = 'varbinary' then 'byte[]'
            when DATA_TYPE = 'varchar' then 'string'
            WHEN DATA_TYPE = 'longtext' THEN 'string'
            else '_UNKNOWN_'
        end, ' ', 
        COLUMN_NAME, ' {get; set;}') as property_
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = @table AND table_schema = @schema) a
;
 
later add Dapper into your project and add Dapper.Contrib
now you don’t need to write a open Datareader and write some reading writing code again and again.
Dapper.Contrib give you some cool functionality like Insert and Update. You still need to write your SQL queries but it’s going to be good and easier to maintain the code. Last year I was working on a c# project and project become full mess of these code. With one line of SELECT , INSERT OR UPDATE code and 100 line of code later to just read those things from DataReader.

Dapper can save a lot of your time doing those same repeated thing and do it pretty well.
https://github.com/StackExchange/Dapper
https://stackoverflow.com/questions/tagged/dapper

#orm
 
Top