Skip to content

Instantly share code, notes, and snippets.

@jbubriski
Created January 13, 2012 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jbubriski/1607389 to your computer and use it in GitHub Desktop.
Save jbubriski/1607389 to your computer and use it in GitHub Desktop.
T4 POCO Generator
<#@ Include File="SaveOutput.tt" #>
<#@ Assembly Name="System.Xml" #>
<#@ Assembly Name="System.Text.RegularExpression" #>
<#@ Assembly Name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ Assembly Name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ Assembly Name="Microsoft.SqlServer.Smo" #>
<#@ Import Namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ Import Namespace="System.Xml.Serialization" #>
<#@ Import Namespace="System.Collections" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Import Namespace="System.Text.RegularExpressions" #>
<#
var namespaceName = "MyNameSpace";
var serverName = "SVDEVDB1";
var databaseName = "Portal";
var excludeTablesPattern = @""; // ex: ^aspnet_
if(!string.IsNullOrWhiteSpace(namespaceName)
&& !string.IsNullOrWhiteSpace(serverName)
&& !string.IsNullOrWhiteSpace(databaseName))
{
var server = new Server(serverName);
var database = server.Databases[databaseName];
foreach (Table table in database.Tables)
{
var shouldIgnoreTable = false;
if(String.IsNullOrWhiteSpace(excludeTablesPattern))
{
shouldIgnoreTable = Regex.IsMatch(table.Name, excludeTablesPattern,
RegexOptions.Singleline |
RegexOptions.IgnoreCase);
}
if(shouldIgnoreTable)
{
continue;
}
else
{
OutputClass(namespaceName, table);
SaveOutput(table.Name + ".cs");
}
}
}
var typeMappings = new Dictionary<string,string>
{
{ "bit","bool" },
{ "uniqueidentifier","Guid" },
{ "datetime","DateTime" },
{ "datetime2","DateTime" },
{ "int","int" },
{ "smallint","short" },
{ "bigint","long" },
{ "varchar","string" },
{ "nvarchar","string" },
{ "text","string" },
{ "ntext","string" }
};
string OutputProperty(Column column)
{
var name = column.Name;
var sqlDataType = column.DataType.Name;
var isNullable = column.Nullable;
var isPrimaryKey = column.InPrimaryKey;
var type = typeMappings.ContainsKey(sqlDataType) ? typeMappings[sqlDataType] : "string";
var typeFormat = type != "string" && isNullable ? "Nullable<{0}>" : "{0}";
return String.Format(
"\t{0} public {1} {2} {{ get; set; }}",
isPrimaryKey ? "[Key]\r\n" : "",
String.Format(typeFormat, type),
name);
}
IEnumerable<string> GatherProperties(ColumnCollection columns)
{
foreach(var col in columns)
{
return yield OutputProperty(col);
}
}
#>
<#+
void OutputClass(string namespaceName, Table table)
{
#>
namespace <#= namespaceName #>
{
using System;
using System.ComponentModel.DataAnnotations;
public class <#= table.Name #>
{
<#+
PushIndent("\t");
var properties = GatherProperties(table.Columns);
#>
<#= String.Join(System.Environment.NewLine, properties) #>
<#+
PopIndent();
}
#>
}
<#+
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment