65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在 Entity Framework 5 中使用 enum?

starIconstarIconstarIconstarIconstarIcon

5.00/5 (8投票s)

2013年11月25日

CPOL

2分钟阅读

viewsIcon

75353

这篇博文展示了如何在 Entity Framework 5 中使用枚举。

什么是枚举?

  • enum 关键字用于声明一个枚举
  • 由一组名为枚举器列表的命名常量组成的独特类型
  • 每个枚举类型都有一个底层类型
  • 枚举元素的默认底层类型是 int
  • 默认情况下,第一个枚举器的值为 0
  • 并且每个后续枚举器的值递增 1
  • EF 5 枚举的类型可以是 Byte、Int16、Int32、Int64 或 SByte 
  • 您可以从原始文章 下载源代码 

让我们来看一个简单的例子  

例如 1

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

在这个枚举中,Sat 是 0,Sun 是 1,Mon 是 2,以此类推

例如 2: 枚举器可以具有初始化器以覆盖默认值 

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

在这个枚举中,元素序列被强制从 1 开始而不是 0

如何在 Entity Framework 5 Code First 中使用枚举?

  • 此功能是 EF 5 的最新改进

步骤 1: 打开 Visual Studio 2012

步骤 2: 文件 ==> 新建项目作为 EF5EnumSupport

New Project as EF5EnumSupport

步骤 3: 右键单击您的解决方案,然后单击管理 NuGet 包...

Manage NuGet Packages

步骤 4: 单击在线选项卡,然后在搜索框中键入 Entity Framework 5

之后选择 EntityFramework 并单击安装

Select EntityFramework

步骤 5: 单击我接受

Click I Accept

在以上步骤之后,以下提到的 DLL(红色)已添加到项目中

DLLs (Red Color)  have added to the Project

步骤 6: 添加 ==> 新类,并将其设为枚举类型 Course

解决方案资源管理器 

Complete project

Course.cs  

namespace EF5EnumSupport.Models
 {
    public enum Course
    {
        Mcsd = 1,
        Mcse,
        Mcsa,
        Mcitp
    }
 }

 

步骤 7: 添加 ==> 新类作为 Student

Student.cs

 namespace EF5EnumSupport.Models {
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Course Course { get; set; }
    }
 }

步骤 8: 创建 DbContext 派生类作为 StudentContext

StudentContext.cs

 using System.Data.Entity;
 namespace EF5EnumSupport.Models
 {
    public class StudentContext : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }
 }

 

步骤 9: 控制台应用程序的 Main 方法如下

Program.cs

using System;
using System.Linq;
using EF5EnumSupport.Models;
namespace EF5EnumSupport
{
  public class Program
  {
     static void Main(string[] args)
     {
       using (var context = new StudentContext())
       {
        context.Students.Add(new Student { Name = "Sampath Lokuge",
        Course = Course.Mcsd });
        context.SaveChanges();
         var studentQuery = (from d in context.Students
                             where d.Course == Course.Mcsd
                             select d).FirstOrDefault();
         if (studentQuery != null)
          {
           Console.WriteLine("Student Id: {0} Name: {1} Course : {2}",
               studentQuery.Id, studentQuery.Name,
               studentQuery.Course);           }
         }
     }
  }
}

 

全新的 enum 支持允许您在实体类中拥有 enum 属性

enum properties in your entity classes

 

步骤 10: 运行控制台应用程序

调试 ==> 无需调试启动 (Ctrl + F5)

Run the Console Application

 

步骤 11: 数据库在哪里?

  • 默认情况下,数据库将在 LocalDB 实例上创建
  • Entity Framework 以派生上下文的完全限定名称命名数据库
  • EF5EnumSupport.Models.StudentContext

    Visual Studio 2012 中的 SQL Server 对象资源管理器如下所示

    SQL Server Object Explorer in Visual Studio 2012

 

查看 Students 表上的数据

View Data on Students Table

 

什么是 LocalDb?

  • Microsoft SQL Server 2012 Express LocalDB 是 SQL Server Express 的一种执行模式,面向程序开发人员
  • LocalDB 安装复制启动 SQL Server 数据库引擎所需的最少文件集

就这样。您已完成。

结论

  • Entity Framework 5 带来了一些改进,Code First 中的枚举支持是其中之一
  • 您可以看到,当您将枚举类型与模型类一起使用时,Visual Studio 2012 会提供开箱即用的智能感知支持
  • 这确实是 Entity Framework 5 的一项了不起的新功能
  • 所以享受它吧

希望这对您有所帮助。非常感谢您的评论和反馈。 

© . All rights reserved.