有没有办法在C#中动态生成枚举?(Is there a way to dynamically generate an Enum in C#?)

编程入门 行业动态 更新时间:2024-10-25 19:31:36
没有办法在C#中动态生成枚举?(Is there a way to dynamically generate an Enum in C#?)

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

tblEvents

事件ID 用户名 日期 事件类型

tblEventTypes

EventTypeID 事件名称

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

在我使用Entity Framework的应用程序中,我为存储过程创建了一个函数import,它将事件添加到tblEvents。 目前,我将事件类型的整数传递给函数import创建的方法。 这对我来说似乎是不可读的,因为你不知道整数意味着什么不看数据库,找到事件类型表,看看它是什么事件。

我想创建一个枚举来表示事件类型。 有没有办法创建一个基于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.

最满意答案

您不能使用EF设计器,但可以使用T4模板生成枚举。

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

更多推荐

本文发布于:2023-07-26 03:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   动态   dynamically   generate   Enum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!