This is a wonderful snippet that generates C# Class from a Table inside SQL Management Studio, just change the table name
declare @TableName sysname = 'TableName' |
declare @Result varchar(max) = 'public class ' + @TableName + ' |
select @Result = @Result + ' |
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; } |
replace(col.name, ' ', '_') ColumnName, |
when 'bigint' then 'long' |
when 'binary' then 'byte[]' |
when 'char' then 'string' |
when 'date' then 'DateTime' |
when 'datetime' then 'DateTime' |
when 'datetime2' then 'DateTime' |
when 'datetimeoffset' then 'DateTimeOffset' |
when 'decimal' then 'decimal' |
when 'float' then 'double' |
when 'image' then 'byte[]' |
when 'money' then 'decimal' |
when 'nchar' then 'string' |
when 'ntext' then 'string' |
when 'numeric' then 'decimal' |
when 'nvarchar' then 'string' |
when 'smalldatetime' then 'DateTime' |
when 'smallint' then 'short' |
when 'smallmoney' then 'decimal' |
when 'text' then 'string' |
when 'time' then 'TimeSpan' |
when 'timestamp' then 'long' |
when 'tinyint' then 'byte' |
when 'uniqueidentifier' then 'Guid' |
when 'varbinary' then 'byte[]' |
when 'varchar' then 'string' |
else 'UNKNOWN_' + typ.name |
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') |
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id |
where object_id = object_id(@TableName) |
set @Result = @Result + ' |
Source: https://stackoverflow.com/questions/5873170/generate-class-from-database-table