使用数据库自动生成实体类的简易方式

SELECT
tab.name AS table_name,
colm.name AS column_name,
typ.name as typename,
prop.value AS column_description,
'/// <summary> '+char(10)+
'/// '+CONVERT(VARCHAR(20),prop.value) +char(10)+
'/// </summary>'+char(10)+
'public '+(CASE CONVERT(VARCHAR(20),typ.name) WHEN 'varchar' THEN 'string' WHEN 'image' THEN 'string' WHEN 'datetime' THEN 'DateTime' WHEN 'decimal' THEN 'decimal' ELSE '' END)+' '+colm.name+' { get; set; }'

FROM sys.tables tab
INNER JOIN sys.columns colm ON colm.object_id = tab.object_id
LEFT JOIN sys.extended_properties prop ON prop.major_id = colm.object_id AND prop.minor_id = colm.column_id
LEFT JOIN sys.types typ ON colm.system_type_id=typ.system_type_id
WHERE tab.name = 'tablename'

posted @ 2018-11-28 20:59  大山楂丸子  阅读(390)  评论(0编辑  收藏  举报