Enum in Entity Framework:

You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should target .NET framework 4.5 in order to use Enum.

Enum can be created for the following data types:

  • Int16
  • Int32
  • Int64
  • Byte
  • SByte

You can create and use an Enum type in your entity data model in three ways:

  1. Convert an existing property of entity to Enum from EDM designer
  2. Add a new Enum from EDM designer
  3. Use an existing Enum type from different namespace

For the purposes of this demo, we have included the TeacherType integer column in the Teacher table of SchoolDB. TeacherType 1 is for permanent teachers, 2 is for contractor teachers, and 3 is for guest teachers.

1. Convert an existing property to Enum:

Next, we will see how to convert a TeacherType to an Enum.

First, right click on the TeacherType property of a Teacher entity and click 'Convert to Enum' in the context menu.

It will open the 'Add Enum Type' dialog box where you can enter the 'Enum Type Name' and, select 'Underlying Type' and Enum member names. For example:

After converting it to Enum, you can see TeacherType as Enum Type in the Model Browser, as shown below:

Also, you can see that the type of the TeacherType property is converted to TeacherType Enum:

Now, you can use TeacherType Enum in CRUD operation using DBContext. For example:

using (var ctx = new SchoolDBEntities())
{
Teacher tchr = new Teacher();
tchr.TeacherName = "New Teacher"; //assign enum value
tchr.TeacherType = TeacherType.Permanent; ctx.Teachers.Add(tchr); ctx.SaveChanges();
}

2. Add New Enum from Designer:

You can also add a new Enum by right clicking on EDM designer and selecting Add → Enum Type. It will open the same 'Add Enum Type' dialog box, where you can enter enum members.

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

3. If you already have Enum type created in your code, then you can use that as a data type of any entity property.

To use an existing Enum type, right click on designer → Add New → Enum Type. Enter the Enum Type Name in the dialog box. Do not enter the member as you already have that 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.

Note: Select 'Set Flags attribute' if you want to use bitwise operators with your Enum.

Entity Framework Tutorial Basics(32):Enum Support的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  4. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. 使用手势对UIImageView进行缩放、旋转和移动(转)

    原文地址:http://blog.csdn.net/crazy_frog/article/details/8664108 // 添加所有的手势 - (void) addGestureRecognize ...

  2. 【整理】C++中的unique函数

    之前总结了一下我觉得有用的erase,lower_bound,upper_bound. 现在总结一下unique,unique的作用是“去掉”容器中相邻元素的重复元素(不一定要求数组有序),它会把重复 ...

  3. 1、Monkey入门准备教程

    1.前提需要Android环境 ADT:链接: https://pan.baidu.com/s/1QN6EJh46cJGvUBaMZjtiWw 密码: a7zu Eclipse:https://www ...

  4. js中call apply方法的使用介绍

    js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, ...

  5. CentOS6.5上安装Python2.7和PIP

    目前大部分用户使用的CentOS6.5上默认的Python还是2.6版本.升级到Python2.7碰到很多问题.本文将介绍如何安装Python2.7. 1. 安装必要的准备包 安装过程将用到gcc,方 ...

  6. 机器学习:PCA(高维数据映射为低维数据 封装&调用)

    一.基础理解 1) PCA 降维的基本原理 寻找另外一个坐标系,新坐标系中的坐标轴以此表示原来样本的重要程度,也就是主成分:取出前 k 个主成分,将数据映射到这 k 个坐标轴上,获得一个低维的数据集. ...

  7. json例子--bai

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. VisualGDB系列10:快速调试Linux应用程序

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何快速调试GCC构建的Linu ...

  9. CreateMutex实现只能打开一个客户端

    #include "stdafx.h" #include <Windows.h> #include <iostream> using namespace s ...

  10. 11-28 网页基础--JavaScript(DOM)

    网页基础 第二部分--HTMLDOM操作 一.定义:htmlDOM是一种面向对象的树的模型,它包含html中的所有元素:通过html可以找到所有包含在dom中的元素. 二.作用: 1.查找html元素 ...