有没有办法在 C# 中动态生成 Enum?

发布于 2024-11-19 02:05:24 字数 597 浏览 2 评论 0原文

在我的应用程序中有两个表。

tblEvents

  • EventID
  • UserID
  • Date
  • EventType

tblEventTypes

  • EventTypeID
  • EventName

tblEvents 包含应用程序中发生的事件的日志。将事件添加到日志时所需的列之一是事件类型。事件类型必须是存储在 tblEventTypes 中的事件类型的 ID。

在使用实体框架的应用程序中,我为存储过程创建了一个函数导入,该存储过程将事件添加到 tblEvents。目前,我将事件类型的整数传递到函数导入创建的方法中。这对我来说似乎难以理解,因为如果不查看数据库、查找事件类型表并查看它是什么事件,您就不知道该整数的含义。

我想创建一个枚举来表示事件类型。有没有办法创建基于 tblEventTypes 中的数据生成的动态枚举?就像 EF 如何根据数据库中的表生成实体一样,我想根据表及其当前数据生成一个枚举。因此,添加事件类型只需向 tblEventTypes 添加一行,然后在 EF 设计器中运行更新或自动更新代码中的枚举即可。

In my application there are two tables.

tblEvents

  • EventID
  • UserID
  • Date
  • EventType

tblEventTypes

  • EventTypeID
  • EventName

tblEvents contains a log of events that occurred in our application. One of the columns required when adding an event to the log is the event type. The event type must be the ID of an event type stored within tblEventTypes.

In my application using Entity Framework I created a function import for a stored procedure that adds events to tblEvents. Currently I pass in an integer for event type into the method created by the function import. This just seems unreadable to me as you have no idea what the integer means without looking at the database, finding the event types table, and seeing what event it was.

I would like to create an enum to represent event types. Is there a way to create a dynamic enum that is generated based on the data in tblEventTypes? Just like how EF generates entities based on tables in the database, I would like to generate an enum based off of a table and it's current data. So adding an event type would be a matter of adding a row to tblEventTypes and then running an update in the EF designer or something that would automatically update my enum in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2024-11-26 02:05:25

您不能使用 EF 设计器来实现此目的,但可以使用 T4 模板< /a> 生成枚举。

You can't use the EF designer for that, but you could use a T4 template to generate the enum.

只是偏爱你 2024-11-26 02:05:25

我已经多次遇到这种情况,我总是做出的决定是应该显式定义事件类型并为每个条目创建一个枚举,例如......

Public enum EventType
{
     Info = 100,
     Error = 200
 }

我将创建一个创建项目的数据库脚本在数据库中并手动保持它们同步。

我不明白将它们用作动态类型有什么意义,因为您只会以显式方式使用它们(例如记录某种类型的事件)。

据我所知 EF 不支持实体记录建模,仅支持模式

I have come across this type of situation quite a few times and the decision i aLways come to is that the event types should be explicitly defined and an enum created for each entry such as...

Public enum EventType
{
     Info = 100,
     Error = 200
 }

I would create a db script that creates the items in the database and keep them in sync manually.

I cant see how it would make sense to use them as a dynamic type as you would only use them in an explicit manner (such as recording an event of a certain type).

To the best of my knowledge EF does not support entity record modelling, only schemas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文