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

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

问题描述

我的要求是根据数据库中表中存在的值创建一个枚举.我正在使用 ADO.NET Entity Framework 模型(.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 连接,但正如您所见,您可以包含任何代码并将您喜欢的任何输出生成到编译到项目中的 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 + "		");
    }
    reader.Close();
    sqlComm.Dispose();
    #>
<#= sb.ToString() #>
    }
}

尝试对此进行改进.将每个 reader.Read() 的结果直接输出到输出,而不是写入 StringBuilder.此外,我还包含了一个尚不存在的 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天全站免登陆