C#枚举和数据库表

Rob*_*ert 2 c# database enums

有没有办法创建一个与数据库表相关的枚举,集合或类似的东西,所以每次添加数据库行时我都不会在代码库中更新我的枚举?...并且强类型.对不起,我不得不扔那里.

Chr*_*ken 7

T4模板,这是我今天早上写的一个,它构建了一个类,并在表中的每个记录中枚举.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ template language="C#v3.5" #>
<#@ output extension="CS" #>
<#@ assembly name="System.Data.dll" #>
<#@ assembly name="System.Xml.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#
   string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Portal;User Id=sa;Password=33321a;";
    DataTable tables = new DataTable("Tables");
    using (SqlConnection connection =  new SqlConnection(connectionString))
    {
    SqlCommand command = connection.CreateCommand();
    command.CommandText = "select * from Rights order by name";
    connection.Open();
    tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
    }
   #>


namespace <#Write(System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString());#>
{
    public partial class RightsList
    {
    <# foreach (DataRow row in tables.Rows){
        string name = row["Name"].ToString();  
        WriteLine("public string "+name+" { get; set; }");
        }#> 

     }  

        public enum Rights
    {
    <# foreach (DataRow row in tables.Rows){
        string name = row["Name"].ToString();  
        WriteLine(name+", ");
        }#> 

     }    

}
Run Code Online (Sandbox Code Playgroud)