Created
January 13, 2012 16:38
-
-
Save jbubriski/1607389 to your computer and use it in GitHub Desktop.
T4 POCO Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ 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