T4, , .
- " " ( "" > " " ), " " ), , " ", :
PM>install-package petapoco
"" "Generated". "" "Database.tt" . http://www.toptensoftware.com/petapoco/.
T4, "Database.cs" . " ". , . , . / , .
- TSQL . , , PetaPoco.
declare @script nvarchar(max);
declare @table nvarchar(256);
set @table = 'YourTableName'
set @script = 'public interface I' + @table + '{' + char(10);
SELECT
@script = @script +
CASE
WHEN st.Name IN ('int') AND c.is_nullable = 0 THEN 'int'
WHEN st.name in ('smallint') AND c.is_nullable = 0 THEN 'short'
WHEN st.name IN ('bigint') AND c.is_nullable = 0 THEN 'long'
WHEN st.name IN ('varchar','nvarchar','sysname') THEN 'string'
WHEN st.Name IN ('datetime') AND c.is_nullable = 0 THEN 'DateTime'
WHEN st.Name IN ('bit') AND c.is_nullable = 0 THEN 'bool'
WHEN st.Name IN ('decimal') AND c.is_nullable = 0 THEN 'decimal'
WHEN st.Name IN ('int') AND c.is_nullable = 1 THEN 'int?'
WHEN st.name in ('smallint') AND c.is_nullable = 1 THEN 'short?'
WHEN st.name IN ('bigint') AND c.is_nullable = 1 THEN 'long?'
WHEN st.name IN ('varchar','nvarchar','sysname') AND c.is_nullable = 1 THEN 'string?'
WHEN st.Name IN ('datetime') AND c.is_nullable = 1 THEN 'DateTime?'
WHEN st.Name IN ('bit') AND c.is_nullable = 1 THEN 'bool?'
WHEN st.Name IN ('decimal') AND c.is_nullable = 1 THEN 'decimal?'
ELSE 'UNKOWN-' + st.name
END
+ ' ' + c.name + '{get;set;}' + char(10)
FROM sys.tables t
INNER JOIN sys.columns c
ON t.object_id = c.object_id
INNER JOIN sys.types st
ON st.system_type_id = c.system_type_id
WHERE t.name = @table
print @script + '}'
, .