Advertisement
uwekeim

Generate POCOs/models from all tables in SQL Server DB

Apr 30th, 2018
4,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.87 KB | None | 0 0
  1. -- Generates POCOs/models for all tables in the current database.
  2. -- Useful for later processing e. g. with Dapper.
  3. --
  4. -- See https://stackoverflow.com/a/5873231/107625 for the original idea
  5. -- See http://midnightprogrammer.net/post/use-sql-query-to-writecreate-a-file for the SP to write to file.
  6.  
  7. declare @TableName sysname
  8. declare @Result nvarchar(MAX) = ''
  9.  
  10.  
  11. DECLARE table_cursor CURSOR FOR
  12. SELECT TABLE_NAME
  13. FROM [INFORMATION_SCHEMA].[TABLES]
  14.  
  15. OPEN table_cursor
  16.  
  17. FETCH NEXT FROM table_cursor
  18. INTO @tableName
  19.  
  20. WHILE @@FETCH_STATUS = 0
  21. BEGIN
  22.  
  23.  
  24. -- https://stackoverflow.com/a/5873231/107625
  25.  
  26. select @Result = @Result + '[Table(@"' + @TableName + '")]
  27. public class ' + @TableName + '
  28. {'
  29.  
  30.     select @Result = @Result + '
  31.    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'
  32.             from
  33.             (
  34.                 select
  35.                     replace(col.name, ' ', '_') ColumnName,
  36.                     column_id ColumnId,
  37.                     case typ.name
  38.                         when 'bigint' then 'long'
  39.                         when 'binary' then 'byte[]'
  40.                         when 'bit' then 'bool'
  41.                         when 'char' then 'string'
  42.                         when 'date' then 'DateTime'
  43.                         when 'datetime' then 'DateTime'
  44.                         when 'datetime2' then 'DateTime'
  45.                         when 'datetimeoffset' then 'DateTimeOffset'
  46.                         when 'decimal' then 'decimal'
  47.                         when 'float' then 'float'
  48.                         when 'image' then 'byte[]'
  49.                         when 'int' then 'int'
  50.                         when 'money' then 'decimal'
  51.                         when 'nchar' then 'string'
  52.                         when 'ntext' then 'string'
  53.                         when 'numeric' then 'decimal'
  54.                         when 'nvarchar' then 'string'
  55.                         when 'real' then 'double'
  56.                         when 'smalldatetime' then 'DateTime'
  57.                         when 'smallint' then 'short'
  58.                         when 'smallmoney' then 'decimal'
  59.                         when 'text' then 'string'
  60.                         when 'time' then 'TimeSpan'
  61.                         when 'timestamp' then 'DateTime'
  62.                         when 'tinyint' then 'byte'
  63.                         when 'uniqueidentifier' then 'Guid'
  64.                         when 'varbinary' then 'byte[]'
  65.                         when 'varchar' then 'string'
  66.                         else 'UNKNOWN_' + typ.name
  67.                     end ColumnType,
  68.                     case
  69.                         when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
  70.                         then '?'
  71.                         else ''
  72.                     end NullableSign
  73.                 from sys.columns col
  74.                     join sys.types typ on
  75.                         col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
  76.                 where object_id = object_id(@TableName)
  77.             ) t
  78.             order by ColumnId
  79.  
  80.             set @Result = @Result  + '
  81. }
  82.            
  83. '
  84.  
  85.  
  86.     FETCH NEXT FROM table_cursor
  87.     INTO @tableName
  88. END
  89. CLOSE table_cursor
  90. DEALLOCATE table_cursor
  91.  
  92. -- http://midnightprogrammer.net/post/use-sql-query-to-writecreate-a-file
  93. EXEC USP_SaveFile @Result, 'C:\Ablage\POCOS.cs'
  94.  
  95. -- PRINT is truncated, therefore see the above saved file for the full POCOs.
  96. print @Result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement