EF5 Onwards Only - The features, APIs, etc. discussed in this page were introduced in Entity Framework 5. If you are using an earlier version, some or all of the information does not apply.

This video and step-by-step walkthrough shows how to use enum types with the Entity Framework Designer. It also demonstrates how to use enums in a LINQ query.

This walkthrough will use Model First to create a new database, but the EF Designer can also be used with the
Database First workflow to map to an existing database.

Enum support was introduced in Entity Framework 5. To use the new features like enums, spatial data types, and table-valued functions, you must target .NET Framework 4.5. Visual Studio 2012 targets .NET 4.5 by default.

In Entity Framework, an enumeration can have the following underlying types: Byte, Int16, Int32, Int64 , or SByte.

The Entity Designer, which provides a design surface for editing your model, is displayed.

The wizard performs the following actions:

Generates the EnumTestModel.edmx file that defines the conceptual model, the storage model, and the mapping between them. Sets the Metadata Artifact Processing property of the .edmx file to Embed in Output Assembly so the generated metadata files get embedded into the assembly.

Adds a reference to the following assemblies: EntityFramework, System.ComponentModel.DataAnnotations, and System.Data.Entity.

Creates EnumTestModel.tt and EnumTestModel.Context.tt files and adds them under the .edmx file. These T4 template files generate the code that defines the DbContext derived type and POCO types that map to the entities in the .edmx model.

Add a New Entity Type

Right-click an empty area of the design surface, select Add -> Entity, the New Entity dialog box appears

Specify Department for the type name and specify DepartmentID for the key property name, leave the type as Int32

Click OK

Right-click the entity and select Add New -> Scalar Property

Rename the new property to Name

Change the type of the new property to Int32 (by default, the new property is of String type)To change the type, open the Properties window and change the Type property to Int32

Add another scalar property and rename it to Budget, change the type to Decimal

Add an Enum Type

In the Entity Framework Designer, right-click the Name property, select Convert to enum

In the Add Enum dialog box type DepartmentNames for the Enum Type Name, change the Underlying Type to Int32, and then add the following members to the type: English, Math, and Economics

Press OK

Save the model and build the projectNote:When you build, warnings about unmapped entities and associations may appear in the Error List. You can ignore these warnings because after we choose to generate the database from the model, the errors will go away.

If you look at the Properties window, you will notice that the type of the Name property was changed to DepartmentNames and the newly added enum type was added to the list of types.

If you switch to the Model Browser window, you will see that the type was also added to the Enum Types node.

Note:You can also add new enum types from this window by clicking the right mouse button and selecting Add Enum Type. Once the type is created it will appear in the list of types and you would be able to associate with a property

Generate Database from Model

Now we can generate a database that is based on the model.

Right-click an empty space on the Entity Designer surface and select Generate Database from Model

The Choose Your Data Connection Dialog Box of the Generate Database Wizard is displayedClick the New Connection buttonSpecify (localdb)\v11.0 for the server name and EnumTest for the database and click OK

A dialog asking if you want to create a new database will pop up, click Yes.

Click Next and the Create Database Wizard generates data definition language (DDL) for creating a databaseThe generated DDL is displayed in the Summary and Settings Dialog BoxNote, that the DDL does not contain a definition for a table that maps to the enumeration type

Click FinishClicking Finish does not execute the DDL script.

The Create Database Wizard does the following:Opens the EnumTest.edmx.sql in T-SQL EditorGenerates the store schema and mapping sections of the EDMX fileAdds connection string information to the App.config file

Click the right mouse button in T-SQL Editor and select ExecuteThe Connect to Server dialog appears, enter the connection information from step 2 and click Connect

To view the generated schema, right-click on the database name in SQL Server Object Explorer and select Refresh

Persist and Retrieve Data

Open the Program.cs file where the Main method is defined. Add the following code into the Main function. The code adds a new Department object to the context. It then saves the data. The code also executes a LINQ query that returns a Department where the name is DepartmentNames.English.