使用 ADO.NET 实体框架从表中存在的值生成枚举

Generate Enum from Values present in a table using ADO.NET Entity framework(使用 ADO.NET 实体框架从表中存在的值生成枚举)
本文介绍了使用 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 实体框架从表中存在的值生成枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)