Entity Framework - Enum


What is enum?
An enumerated type (also called enumeration, enum) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names usually behave as constants in the language. An enumerated type has values that are different from each other,and that can be compared and assigned. The names of enumerators need not be semantically complete or compatible in any sense. Values and variables of an enumerated type are usually implemented as fixed-length bit strings, often in a format and size compatible with some integer type. 

The security and reliability of an application both increase when use enumerations.
In Entity Framework 5 onward, an enumeration can have the following underlying types -

Byte
Int16
Int32
Int64
SByte

The default underlying type of the enumeration elements is int. 
By default, the first enumerator has the value 0 and the value of each successive enumerator is increased by 1

You can create and use Enum type in Entity Data Model (EDM) in three ways:

1. Add new Enum from Entity Data Model (EDM) designer
2. Convert an existing property of entity to Enum from Entity Data Model (EDM) designer
3. Use an existing Enum type from different namespace


1. Add new Enum from Entity Data Model (EDM) designer
New Enum can be added by right clicking on EDM designer and selecting Add → Enum Type. It will open the Add Enum Type dialog box, where you can enter enum members.

Entity Framework: Enum

After creating Enum Type you can change the type of the MessageType property to the newly created MessageType Enum from the property window.

2. Convert an existing property of entity to Enum from Entity Data Model (EDM) designer
In the EDM, right click on MessageType property of DU_FND_Message entity and click on "Convert to Enum" option in the context menu.

Entity Framework: Enum

It will open the 'Add Enum Type' dialog box where you have to fill 'Enum Type Name' and, select 'Underlying Type' and Enum member names, Value (optional). 

Entity Framework: Enum

After converting to Enum, you can see MessageType as Enum Type in the Model Browser:

Entity Framework: Enum

 

Also, the type of the MessageType property is converted to MessageType Enum

Entity Framework: Enum

MessageType Enum in CRUD operation using DBContext. 

using (var ctx = new DemoDBEntities())
    {
        DU_FND_Message nmsg = new DU_FND_Message();
        nmsg.FromID = 5;
        nmsg.ToID = 6;
        nmsg.Message = "Message content";
        nmsg.MessageType = MessageType.Personal;//Enum used
        ctx.DU_FND_Message.Add(nmsg);
        ctx.SaveChanges();
    }

3. Use an existing Enum type from different namespace
To use an existing Enum type, right click on EDM Designer → Add New → Enum Type. Enter the Enum Type Name in the dialog box. Do not enter the enum member becuase members are already have in your code.

Now, select 'Reference external type' checkbox and enter the namespace of your existing enum and click OK. This will add the Enum type in the Model browser. Then, you can assign this Enum type to any property of an entity from the property window. Select 'Set Flags attribute' if want to use bitwise operators with Enum.

Entity Framework: Enu
 

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More