从使用ADO.NET实体框架的表中存在的值生成枚举 [英] Generate Enum from Values present in a table using ADO.NET Entity framework

查看:204
本文介绍了从使用ADO.NET实体框架的表中存在的值生成枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是根据DB中存在的值创建枚举。我正在使用ADO.NET实体框架模型(.edmx文件),可以帮助我任何一个。

My requirement is to create an Enum based on values present in a table from DB. I am using ADO.NET Entity Framework model (.edmx file), Can any one of you help me out.

推荐答案

使用T4模板可能比较容易。 这是一篇非常好的关于入门的文章

It is probably a lot easier to use T4 templates. Here is a really good article on getting started

下面的示例使用直接的SQL Connection,但是您可以看到,您可以包含任何代码,并将任何您喜欢的输出生成到编译到项目中的cs文件中。您可以将下面的ADO语法替换为通过您的Entituy Framework模型检索的对象集合的枚举,并相应地输出。

My example below uses a direct SQL Connection, but as you can see you can include any code and generate whatever output you like into a cs file that is compiled into your project. You could replace the ADO syntax below with an enumeration over a collection of objects retrieved via your Entituy Framework model and output accordingly.

在您想要生成枚举文件的目录中创建一个扩展名为.tt的文件。如果您将文件命名为XXXXX.tt,则将生成一个名为XXXXX.cs的文件,以便适当地命名tt文件。

Create a file with the extension .tt in the directory where you would like the enumeration file to be generated. If you name the file XXXXX.tt then a file called XXXXX.cs will be generated so, name the tt file appropriately.

尝试这些行。您可能需要使用语法和输出进行一些实验,但我不会为您编写所有内容,或者您​​不会学习任何内容:)

Try something along these lines. You might need to experiment a little with the syntax and the output, but I'm not going to write it all for you or you won't learn anything :)

只需要知道,这个数据库调用将在每次编辑tt文件时进行。

Just be aware, that this database call will be made every time you edit the tt file.

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="cs" #>
<#@ assembly name="System.Data" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#
    SqlConnection sqlConn = new SqlConnection(@"Data Source=XXXX;Initial Catalog=XXXX; Integrated Security=True");
    sqlConn.Open();
#>
namespace AppropriateNamespace
{
public enum YourEnumName
{
    <#
    string sql = string.Format("SELECT Id, Name FROM YourTable ORDER BY Id");
    SqlCommand sqlComm = new SqlCommand(sql, sqlConn);
    IDataReader reader = sqlComm.ExecuteReader();

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    while (reader.Read())
    {
        sb.Append(FixName(reader["Name"].ToString()) + " = " + reader["Id"] + "," + Environment.NewLine + "\t\t");
    }
    reader.Close();
    sqlComm.Dispose();
    #>
<#= sb.ToString() #>
    }
}

尝试改进这一点。而不是写入StringBuilder,将每个reader.Read()的结果直接输出到输出。另外,我已经包含一个尚不存在的FixName方法,但是您可能需要这样做来取出空格或非法字符。

Try improving on this. Rather than writing to a StringBuilder, output the results of each reader.Read() directly to the output. Also, I have included a FixName method that doesn't exist yet, but you might need that to take out spaces or illegal characters.

这篇关于从使用ADO.NET实体框架的表中存在的值生成枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆