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. linux 缺少libxxx.a 静态链接库

    首先去官方网站下载gdb的源码包,我下载的7.4.1版本的源码包,解压开来,进入到源码包的根目录下.对于一个源码包,拿到手里首先要阅读的就是README,然后看一下INSTALL文件,这个文件里编译源 ...

  2. python沉淀之路~~整型的属性

    python的基础知识: 基本数据类型:int   str   list   tuple   dict   bool 一.整型的属性功能 1.工厂方法将字符串转换成整型 a = " b = ...

  3. php 服务器的安全笔记

    php 服务器的安全笔记 操作系统安全 默认端口修改 MySQL 端口禁止外网访问 用户权限 父进程 子进程 目录权限 TODO Web Server 版本信息 服务器版本信息 PHP 版本 open ...

  4. 基于JDK1.7.0_80与JDK1.8.0_66做的分析

    JDK1.7中 使用一个Entry数组来存储数据,用key的hashcode取模来决定key会被放到数组里的位置,如果hashcode相同,或者hashcode取模后的结果相同(hash collis ...

  5. openwrt 按下回车才能显示图标信息

    如题所示,openwrt启动后,手动才能按下系统图标和信息. 如何却掉这个手动选项呢? 修改文件/SISP-L26.7.8-OpenWrt/build_dir/target-arm_uClibc-0. ...

  6. ORACLE显式授权

    同一数据库 两个不同用户 user1 user2 user1里面有一张表 table1 在user2里面创建synonymcreate synonym sy1 for user1.table1; 创建 ...

  7. 机器学习:SVM(scikit-learn 中的 SVM:LinearSVC)

    一.基础理解 Hard Margin SVM 和 Soft Margin SVM 都是解决线性分类问题,无论是线性可分的问题,还是线性不可分的问题: 和 kNN 算法一样,使用 SVM 算法前,要对数 ...

  8. Spark Tungsten in-heap / off-heap 内存管理机制--待整理

    一:Tungsten中到底什么是Page? 1. 在Spark其实不存在Page这个类的.Page是一种数据结构(类似于Stack,List等),从OS层面上讲,Page代表了一个内存块,在Page里 ...

  9. Python 下载图片的几种方法

    import osos.makedirs('./image/', exist_ok=True)IMAGE_URL = "http://image.nationalgeographic.com ...

  10. vue axios 应用

    vue安装axios cnpm install axios 安装成功后/项目/node_modules/目录下有axios文件夹 在package.json文件中devDependencies字段中添 ...