Entity Framework Tutorial Basics(32):Enum Support
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:
- Convert an existing property of entity to Enum from EDM designer
- Add a new Enum from EDM designer
- 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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- hdu1863(最小生成树)
很裸的最小生成树,但要注意判断输出问号的情况.其实就是当给的图不是连通图时输出问号.判断方法是:看形成的最小生成树的边数是不是等于节点数减一. #include<iostream> #in ...
- Shiro-RememberMe
概述 认证和记住我 建议 身份验证相关 实现 如果要自己做RememeberMe,需要在登录之前创建Token:UsernamePasswordToken(用户名,密码,是否记住我),且调用 User ...
- BZOJ4861 [Beijing2017]魔法咒语
题意 Chandra 是一个魔法天才.从一岁时接受火之教会洗礼之后, Chandra 就显示出对火元素无与伦比的亲和力,轻而易举地学会种种晦涩难解的法术.这也多亏 Chandra 有着常人难以企及的语 ...
- arm linux 下移植busybox 的tftp
(1)进入busybox目录,make menuconfig ,然后在networking中勾选tftp项跟tftpd项. (2)配置/etc/inetd.conf 中关于tftp的选项(此部未验证, ...
- laravel 配置文件的使用
在开发的时候有许多数据是固定的 或者是多处使用的, 那么我们可以把它保存到配置文件中, 这样将来我们可以直接从配置文件中读取这个数据,如果有特殊的数据需要改变的时候,我们也可以在单独特定的环境中,不使 ...
- Windows命令查看文件的MD5/SHA1/SHA256
certutil -hashfile "D:\Tools\Microsoft\SqlServer\2016\ct_sql_server_2016_enterprise_x64_dvd_869 ...
- JSON-lib框架,转换JSON、XML
json-lib工具包 下载地址: http://sourceforge.net/projects/json-lib/json-lib还需要以下依赖包: jakarta commons-lang 2. ...
- (转)AppCan中调用系统浏览器打开网页
<!DOCTYPE html> <html> <head> <style>body{ background:#fff; font-size:30px;} ...
- [Angularjs-学习笔记]工具篇
因为一开始学习前端知识一直都是在慕课网,所以这次准备学习下angularjs等了好久,终于慕课网出了angularjs的内容,于是准备开始跟着老师的步骤进行学习. 大漠老师关于开发工具的内容讲得比较快 ...
- oralce 记一次 External Procedure initial connection 处理
1 环境 oracle 11.2.0.4 RAC(2 nodes),centos 6.8,实体机 2 问题 线上环境执行一条sql sql> select ST_AsText(ST_Geomet ...