Last active
July 25, 2025 05:49
-
-
Save pipiscrew/d9a02e59e71c44829bdf61910cdcd392 to your computer and use it in GitHub Desktop.
Generate c# POCO classes from SQL Server database
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
--fixed version of | |
--SOURCE : https://gist.github.com/joey-qc/6710702?permalink_comment_id=5345514#gistcomment-5345514 | |
--* PRIMARY KEY appears double/triple/tenple | |
--* FOREIGN KEY removed (useless) | |
DECLARE @tableName varchar(200) | |
DECLARE @columnName varchar(200) | |
DECLARE @nullable varchar(50) | |
DECLARE @datatype varchar(50) | |
DECLARE @maxlen int | |
DECLARE @description nvarchar(500) | |
DECLARE @isPrimaryKey bit | |
DECLARE @isForeignKey bit | |
DECLARE @Stype varchar(50) | |
DECLARE @Sproperty varchar(200) | |
DECLARE table_cursor CURSOR FOR | |
SELECT TABLE_NAME | |
FROM [INFORMATION_SCHEMA].[TABLES] | |
OPEN table_cursor | |
FETCH NEXT FROM table_cursor INTO @tableName | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
PRINT 'public class ' + @tableName + ' {' | |
DECLARE column_cursor CURSOR FOR | |
SELECT | |
c.COLUMN_NAME, | |
c.IS_NULLABLE, | |
c.DATA_TYPE, | |
ISNULL(c.CHARACTER_MAXIMUM_LENGTH, '-1') AS MaxLen, | |
ISNULL(CAST(ep.value AS NVARCHAR(MAX)), '') AS Description, | |
CASE WHEN pk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS IsPrimaryKey | |
--CASE WHEN fk.parent_column_id IS NOT NULL THEN 1 ELSE 0 END AS IsForeignKey | |
FROM [INFORMATION_SCHEMA].[COLUMNS] c | |
LEFT JOIN sys.columns sc ON sc.name = c.COLUMN_NAME | |
AND sc.object_id = OBJECT_ID(@tableName) | |
LEFT JOIN sys.extended_properties ep ON ep.major_id = sc.object_id | |
AND ep.minor_id = sc.column_id | |
AND ep.name = 'MS_Description' | |
LEFT JOIN ( | |
SELECT kcu.COLUMN_NAME, tc.TABLE_NAME | |
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc | |
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu | |
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME | |
WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY' | |
) pk ON pk.COLUMN_NAME = c.COLUMN_NAME and pk.table_name = @tableName | |
--LEFT JOIN ( | |
-- SELECT fkc.parent_column_id, fkc.parent_object_id | |
-- FROM sys.foreign_key_columns fkc | |
--) fk ON fk.parent_column_id = sc.column_id | |
-- AND fk.parent_object_id = OBJECT_ID(@tableName) | |
WHERE c.TABLE_NAME = @tableName | |
ORDER BY c.ORDINAL_POSITION | |
OPEN column_cursor | |
FETCH NEXT FROM column_cursor | |
INTO @columnName, @nullable, @datatype, @maxlen, @description, @isPrimaryKey--, @isForeignKey | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
-- 資料型別轉換 | |
SELECT @sType = CASE @datatype | |
WHEN 'int' THEN 'int' | |
WHEN 'decimal' THEN 'Decimal' | |
WHEN 'money' THEN 'Decimal' | |
WHEN 'char' THEN 'string' | |
WHEN 'nchar' THEN 'string' | |
WHEN 'varchar' THEN 'string' | |
WHEN 'nvarchar' THEN 'string' | |
WHEN 'uniqueidentifier' THEN 'Guid' | |
WHEN 'datetime' THEN 'DateTime' | |
WHEN 'bit' THEN 'bool' | |
ELSE 'string' | |
END | |
-- 輸出 XML 格式的 summary 註解,包含描述、主鍵和外鍵資訊 | |
PRINT ' /// <summary>' | |
PRINT ' /// ' + @columnName | |
IF (LEN(@description) > 0) | |
PRINT ' /// ' + @description | |
IF (@isPrimaryKey = 1) | |
PRINT ' /// 主鍵 (Primary Key)' | |
IF (@isForeignKey = 1) | |
PRINT ' /// 外鍵 (Foreign Key)' | |
PRINT ' /// </summary>' | |
-- Required 註解 | |
IF (@nullable = 'NO') | |
PRINT ' [Required]' | |
-- MaxLength 註解 | |
IF (@sType = 'String' AND @maxlen <> '-1') | |
PRINT ' [MaxLength(' + CONVERT(varchar(4), @maxlen) + ')]' | |
-- 屬性輸出 | |
SELECT @sProperty = ' public ' + @sType + ' ' + @columnName + ' { get; set; }' | |
PRINT @sProperty | |
PRINT '' | |
FETCH NEXT FROM column_cursor | |
INTO @columnName, @nullable, @datatype, @maxlen, @description, @isPrimaryKey --, @isForeignKey | |
END | |
CLOSE column_cursor | |
DEALLOCATE column_cursor | |
PRINT '}' | |
PRINT '' | |
FETCH NEXT FROM table_cursor INTO @tableName | |
END | |
CLOSE table_cursor | |
DEALLOCATE table_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment