entity-framework/ef6/modeling/designer/data-types/enums.md
[!NOTE] 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.
This video shows how to use enum types with the Entity Framework Designer. It also demonstrates how to use enums in a LINQ query.
Presented By: Julia Kornich
You will need to have Visual Studio 2012, Ultimate, Premium, Professional, or Web Express edition installed to complete this walkthrough.
The Entity Designer, which provides a design surface for editing your model, is displayed.
The wizard performs the following actions:
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 project
[!NOTE] 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
Now we can generate a database that is based on the model.
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.
using (var context = new EnumTestModelContainer())
{
context.Departments.Add(new Department{ Name = DepartmentNames.English });
context.SaveChanges();
var department = (from d in context.Departments
where d.Name == DepartmentNames.English
select d).FirstOrDefault();
Console.WriteLine(
"DepartmentID: {0} and Name: {1}",
department.DepartmentID,
department.Name);
}
Compile and run the application. The program produces the following output:
DepartmentID: 1 Name: English
To view data in the database, right-click on the database name in SQL Server Object Explorer and select Refresh. Then, click the right mouse button on the table and select View Data.
In this walkthrough we looked at how to map enum types using the Entity Framework Designer and how to use enums in code.