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. LeetCode Distribute Candies

    原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...

  2. plsql Developer----plsql软件总结(待续更新)

    如何查看数据库所有的表 打开pl/sql Developer连接上数据库了以后,在菜单 Tools 下面有个 Object browser 将其打勾(如果已经打勾了就不用管了),之后在IDE的左边有个 ...

  3. PHP实现图片压缩的两则实例(转)

    本文介绍了PHP实现图片压缩的两种方法,读者可以根据具体应用参考或加以改进,以适应自身应用需求!废话不多说,主要代码部分如下: 实例1: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  4. Java栈,队列,优先队列的使用

    1. 栈的使用: import java.util.*; public class Main{ public static void main(String[] args){ Deque<Str ...

  5. [转]ubuntu11.04配置nfs--解决mount.nfs: access denied问题

    总算通过了nfs的localhost测试. 配置很简单,下面摘自网络,并且整理下: 1 安装nfs #apt-get install nfs-kernel-server #apt-get instal ...

  6. VI与VIM区别

    Vim是从 vi 发展出来的一个文本编辑器 .代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和Emacs 并列成为类Unix系统 用户最喜欢的编辑器. Vim的第一个版本由B ...

  7. [Angularjs-学习笔记]工具篇

    因为一开始学习前端知识一直都是在慕课网,所以这次准备学习下angularjs等了好久,终于慕课网出了angularjs的内容,于是准备开始跟着老师的步骤进行学习. 大漠老师关于开发工具的内容讲得比较快 ...

  8. HotSpotVM创建过程(JNI_CreateJavaVM)详解

    来自:<Java Performance>第3章 JVM Overview The HotSpot VM's implementation of the JNI_CreateJavaVM ...

  9. doker 笔记(1) 架构

    Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...

  10. Tiny4412 Linux 内核启动流程

    Linux内核的启动分为压缩内核和非压缩内核两种,这里我们以压缩内核为例.压缩内核运行时,将运行一段解压缩程序,得到真正的内核镜像,然后跳转到内核镜像运行.此时,Linux进入非压缩内核入口,在非压缩 ...