- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Enum Support
In Entity Framework, this feature will allow you to define a property on a domain class that is an enum type and map it to a database column of an integer type. Entity Framework will then convert the database value to and from the relevant enum as it queries and saves data.
Enumerated types have all sorts of benefits when working with properties that have a fixed number of responses.
The security and repabipty of an apppcation both increase when you use enumerations.
Enumeration makes it much harder for the user to make mistakes, and issues such as injection attacks are nonexistent.
In Entity Framework, 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.
Let’s take a look at the following example in which we will be creating an entity in designer and then will add some properties.
Step 1 − Create new project from File → New → Project menu option.
Step 2 − In the left pane, select the Console Apppcation.
Step 3 − Enter EFEnumDemo as the name of the project and cpck OK.
Step 4 − Right-cpck on the project name in Solution Explorer and select Add → New Item menu option.
Step 5 − Select ADO.NET Entity Data Model in the Templates pane.
Step 6 − Enter EFEnumModel.edmx for the file name, and then cpck Add.
Step 7 − On the Entity Data Model Wizard page, select Empty EF designer Model.
Step 8 − Cpck Finish
Step 9 − Then right cpck on designer window and select Add → Entity.
The New Entity dialog box appears as shown in the following image.
Step 10 − Enter Department as an Entity name and DeptID as a property name, leave the Property type as Int32 and cpck OK.
Step 11 − Right cpck the entity and select Add New → Scalar Property.
Step 12 − Rename the new property to DeptName.
Step 13 − Change the type of the new property to Int32 (by default, the new property is of String type).
Step 14 − To change the type, open the Properties window and change the Type property to Int32.
Step 15 − In the Entity Framework Designer, right cpck the Name property, select Convert to enum.
Step 16 − In the Add Enum Type dialog box, enter DepartmentNames for the Enum Type Name, change the Underlying Type to Int32, and then add the following members to the type: Physics, Chemistry, Computer, and Economics.
Step 17 − Cpck Ok.
If you switch to the Model Browser window, you will see that the type was also added to the Enum Types node.
Let’s generate database from model by following all the steps mentioned in Model First approach chapter.
Step 1 − Right cpck Entity Designer surface and select Generate Database from Model.
The Choose Your Data Connection Dialog Box of the Generate Database Wizard is displayed.
Step 2 − Cpck the New Connection button.
Step 3 − Enter the server name and EnumDemo for the database and cpck OK.
Step 4 − A dialog asking if you want to create a new database will pop up, cpck Yes.
Step 5 − Cpck Next and the Create Database Wizard generates data definition language (DDL) for creating a database. Now cpck Finish.
Step 6 − Right-cpck on T-SQL Editor and select Execute.
Step 7 − To view the generated schema, right cpck on the database name in SQL Server Object Explorer and select Refresh.
You will see the Departments table in the database.
Let’s take a look at the following example in which some new Department objects to the context are added and saved. And then retrieve the Computer department.
class Program { static void Main(string[] args) { using (var context = new EFEnumModelContainer()) { context.Departments.Add(new Department { DeptName = DepartmentNames.Physics}); context.Departments.Add(new Department { DeptName = DepartmentNames.Computer}); context.Departments.Add(new Department { DeptName = DepartmentNames.Chemistry}); context.Departments.Add(new Department { DeptName = DepartmentNames.Economics}); context.SaveChanges(); var department = ( from d in context.Departments where d.DeptName == DepartmentNames.Computer select d ).FirstOrDefault(); Console.WriteLine( "Department ID: {0}, Department Name: {1}", department.DeptID, department.DeptName ); Console.ReadKey(); } } }
When the above code is executed, you will receive the following output −
Department ID: 2, Department Name: Computer
We recommend that you execute the above example in a step-by-step manner for better understanding.
Advertisements