Is there a way to dynamically generate Enum in C #?

There are two tables in my application.

tblEvents

  • Eventid
  • User ID
  • the 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 type of event. The event type must be an identifier for the type of event stored in tblEventTypes.

In my application using the Entity Framework, I created an import function for a stored procedure that adds events to tblEvents. I am currently passing an integer for an event type to the method created by the import function. This seems unreadable to me, since you have no idea what an integer means without looking at the database, finding the table of event tables and seeing what kind of event it is.

I would like to create an enumeration to represent event types. Is there a way to create a dynamic enumeration that is created based on data in tblEventTypes? In the same way that EF generates entities based on tables in a database, I would like to create an enumeration based on a table and current data. Therefore, adding an event type will be associated with adding a string to tblEventTypes, and then with the launch of the update in the EF designer or with the fact that it will automatically update my listing in the code.

+4
source share
2 answers

You cannot use the EF constructor for this, but you can use the T4 template to generate an enumeration.

+3
source

I came across a situation of this type quite a few times, and the solution I came to is that the types of events must be explicitly defined, and an enumeration is created for each record, such as ...

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

I would create a db script that creates the elements in the database and synchronizes them manually.

I cannot understand how it would be wise to use them as a dynamic type, since you would use them only explicitly (for example, recording an event of a certain type).

As far as I know, EF does not support modeling entity records, only schemas

+1
source

All Articles