Database-Table-Wrapper Class Code Generator

The following code references the DbLayout view, available here.

with DataTypes as (
    select TSqlType = 'bigint', CsType = 'long'
    union select 'bit', 'bool'
    union select '%char%', 'string'
    union select '%date%', 'DateTime'
    union select 'decimal%', 'decimal'
    union select 'float%', 'decimal'
    union select 'int', 'int'
    union select 'money', 'decimal'
    union select '%text%', 'string'
    union select 'smallint', 'short'
    union select 'uniqueidentifier','Guid'
)
,Fields as (
    select
         CsType = t.CsType + case when d.IsNullable = 1 and t.CsType <> 'string' then '?' else '' end
        ,d.* 
    from dbo.DbLayout d
        left join DataTypes t
            on d.DataType like t.TSqlType
    where 1=1
        and TableName in ('Order', 'Invoice')
    )
select distinct TableName, Sort2 = 10, Sort3 = 10, code = 'public class ' + TableName + 'View' from Fields 
union select distinct TableName, 20, 10, '{' from Fields
union select TableName, 30, ColumnOrder, code = '    public ' + CsType + ' ' + ColumnName + ' { get; set; }' from Fields
union select distinct TableName, 40, 10, '}' from Fields
order by TableName, Sort2, Sort3