Question

I have tables named Events and Log. The Events table comprises of ID, Event description and type. And, the Log contains LogID, EventID(refers to ID from Event table), and timstamp.

I am using Entities framework. While making an entry in the log table, i mention the event id as the number corresponding to the log event from the Event table. As i continue to use this logging across the app, it becomes cumbersome to remember the event id for the logged event. I would like to map these event ID as enums in my code and just use the enum for better usability.

Has anyone created enums using this approach? If so, please share your thoughts on the design for creating one.

Was it helpful?

Solution

It's possible, but you'll have to define the enum manually.

Whether this is good design depends on the amount of different events you are defining, and when. Is it possible to create new event definitions and not have to change your code? In that case, an enum would probably be a bad idea. If the list of event IDs is static and not too big, then an enum would be acceptable, and (in my opinion) even encourageable.

Edit: my apologies, the solution I provided was actually the way it's done in LINQ to SQL, not Entity Framework 1.0. Must have been a lack of coffee. The way it's done in EF, is to define a properly typed property in a partial class.

For example, I have a column EventType in my database, which I want to map to an enum. I've renamed the property from the default EventType to EventTypeInt, as you can see below:

EventCalendar table mapping in model
(source: subbot.net)

Then you set the Getter and Setter properties for this column to private (in the property manager window which you probably used to rename EventType). Once that's done and saved, use the following code:

public enum EventType
{
    Unknown = 0,
    Concert = 1,
    Festival = 2
}

public partial class EventCalendar
{
    public EventType EventType
    {
        get { return (EventType)EventTypeInt; }
        set { EventTypeInt = (int)value; }
    }
}

Yes, it's tedious. It was a lot more practical in LINQ to SQL models.

OTHER TIPS

Rather than creating ENUMS have you looked into representing them as navigation properties in your entity sets?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top