Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have written one for my needs that converts a lookup table of your choice to an enum: Put this code inside an <strong>EnumGenerator.ttinclude</strong> file:</p> <pre><code>&lt;#@ template debug="true" hostSpecific="true" #&gt; &lt;#@ output extension=".generated.cs" #&gt; &lt;#@ Assembly Name="System.Data" #&gt; &lt;#@ import namespace="System.Data" #&gt; &lt;#@ import namespace="System.Data.SqlClient" #&gt; &lt;#@ import namespace="System.IO" #&gt; &lt;#@ import namespace="System.Text.RegularExpressions" #&gt; &lt;# string tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile); string path = Path.GetDirectoryName(Host.TemplateFile); string columnId = tableName + "ID"; string columnName = "Name"; string connectionString = "data source=.;initial catalog=DBName;integrated security=SSPI"; #&gt; using System; using System.CodeDom.Compiler; namespace Services.&lt;#= GetSubNamespace() #&gt; { /// &lt;summary&gt; /// &lt;#= tableName #&gt; auto generated enumeration /// &lt;/summary&gt; [GeneratedCode("TextTemplatingFileGenerator", "10")] public enum &lt;#= tableName #&gt; { &lt;# SqlConnection conn = new SqlConnection(connectionString); string command = string.Format("select {0}, {1} from {2} order by {0}", columnId, columnName, tableName); SqlCommand comm = new SqlCommand(command, conn); conn.Open(); SqlDataReader reader = comm.ExecuteReader(); bool loop = reader.Read(); while(loop) { #&gt; /// &lt;summary&gt; /// &lt;#= reader[columnName] #&gt; configuration setting. /// &lt;/summary&gt; &lt;#= Pascalize(reader[columnName]) #&gt; = &lt;#= reader[columnId] #&gt;&lt;# loop = reader.Read(); #&gt;&lt;#= loop ? ",\r\n" : string.Empty #&gt; &lt;# } #&gt; } } &lt;#+ private string Pascalize(object value) { Regex rx = new Regex(@"(?:[^a-zA-Z0-9]*)(?&lt;first&gt;[a-zA-Z0-9])(?&lt;reminder&gt;[a-zA-Z0-9]*)(?:[^a-zA-Z0-9]*)"); return rx.Replace(value.ToString(), m =&gt; m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower()); } private string GetSubNamespace() { Regex rx = new Regex(@"(?:.+Services\s)"); string path = Path.GetDirectoryName(Host.TemplateFile); return rx.Replace(path, string.Empty).Replace("\\", "."); } #&gt; </code></pre> <p>Then whenever you'd like an enum to be generated, just create a <strong>tt</strong> file with the same name as database table like <strong>UserType.tt</strong> and put this code in:</p> <pre><code>&lt;#@ include file="..\..\T4 Templates\EnumGenerator.ttinclude" #&gt; </code></pre> <p>An even more advanced template is now available in my <a href="http://erraticdev.blogspot.com/2011/01/generate-enum-of-database-lookup-table.html" rel="noreferrer">blog post</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload