Created
July 25, 2025 05:48
-
-
Save pipiscrew/9111d5307cd4005de7bb538e7cb6d4be to your computer and use it in GitHub Desktop.
Generate c# POCO classes from MYSQL table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment