详解ADO.NET Entity Framework 4中枚举的使用

开发 后端
在Visual Studio 2010正式发布后,.NET 4的重要组件ADO.NET Entity Framework 4也浮出水面。本文将为大家介绍其中关于枚举的使用。

本文将通过ADO.NET Entity Framework 4中枚举的使用介绍,带领大家走进ADO.NET的世界。

枚举(Enum)是一种常用的类型,如用于表示状态、类型等参数。但目前它不会被官方地在ADO.NET Entity Framework中进行支持。本文介绍的是通过复杂类型(Complex Types)在ADO.NET Entity Framework 4中使用枚举。

这种方法需要使用POCO类,而不能使用Visual Studio自动生成的类。因为我们需要手动为复杂类型编写代码。

数据库脚本:

  1. if exists (select 1  
  2.              from  sysobjects  
  3.             where  id = object_id('Account')  
  4.              and   type = 'U')  
  5.     drop table Account  
  6.  go  
  7.    
  8.  create table Account (  
  9.     ID                   uniqueidentifier     not null default NewSequentialID(),  
  10.    UserName             nvarchar(20)         not null,  
  11.    Password             varchar(40)          not null,  
  12.   Email                nvarchar(100)        not null,  
  13.    Role                 int                  not null,  
  14.    constraint PK_ACCOUNT primary key (ID)  
  15. )  
  16.  
  17. insert into Account (UserName ,Password,Email ,Role ) values ('Test1','Test1','test1',1)  
  18. insert into Account (UserName ,Password,Email ,Role ) values ('Test2','Test2','test2',1)  
  19. insert into Account (UserName ,Password,Email ,Role ) values ('Test3','Test3','test3',2) 

这是一个用于存放帐号信息的数据表,Role是个枚举类型,在数据库中用int类型。

我们按常规做法写一个用于表示Role的枚举类型

  1. public enum AccountRoleEnum  
  2. {  
  3.     Admin = 1,  
  4.     User = 2  

然后写一个复杂类型用于在枚举类型和数据库的int类型之间做变换。复杂类型只有在ADO.NET Entity Framework 4中才有。

  1. public partial class RoleWrapper  
  2.  {  
  3.      private AccountRoleEnum m_orderStatus;  
  4.    
  5.      public int Value  
  6.      {  
  7.          get { return (int)m_orderStatus; }  
  8.          set { m_orderStatus = (AccountRoleEnum)value; }  
  9.      }  
  10.  
  11.     public AccountRoleEnum EnumValue  
  12.     {  
  13.         get { return m_orderStatus; }  
  14.         set { m_orderStatus = value; }  
  15.     }  
  16.  
  17.     public static implicit operator RoleWrapper(AccountRoleEnum role)  
  18.     {  
  19.         return new RoleWrapper { EnumValue = role };  
  20.     }  
  21.  
  22.     public static implicit operator AccountRoleEnum(RoleWrapper role)  
  23.     {  
  24.         if (role == null)  
  25.             return AccountRoleEnum.User;  
  26.         return role.EnumValue;  
  27.     }  

最后的2个方法用于隐式类型重载,也就是对类型进行变换。
然后我们写Account实体。

  1. public class Account  
  2. {  
  3.     public Guid ID { getset; }  
  4.     public string UserName { getset; }  
  5.     public string Password { getset; }  
  6.     public string Email { getset; }   
  7.    public RoleWrapper Role { getset; }  

和实体框架上下文。

  1. public class EntitiesContext : ObjectContext  
  2.  {  
  3.      public EntitiesContext()  
  4.          : base("name=Entities""Entities")  
  5.     {  
  6.          _accounts = CreateObjectSet<Account>();  
  7.      }  
  8.    
  9.      public ObjectSet<Account> Accounts  
  10.     {  
  11.         get 
  12.         {  
  13.             return _accounts;  
  14.         }  
  15.     }  
  16.     private ObjectSet<Account> _accounts;  

这样,主要的工作就已经完成了,在比较时可以使用

  1. account.Role == AccountRoleEnum.Admin 

但是在涉及到数据库的查询时,这样的写法是会报错的,只能使用

  1. EntitiesContext db = new EntitiesContext();  
  2. db.Accounts.Where(c => c.Role.Value == (int)AccountRoleEnum.Admin); 
原文标题:在 ADO.NET Entity Framework 4 中使用枚举
链接:http://www.cnblogs.com/snowdream/archive/2010/04/19/use-enum-in-adonet-entity-framework-4.html
责任编辑:彭凡 来源: 博客园
相关推荐

2009-12-22 14:46:09

ADO.NET Ent

2009-11-03 16:27:43

ADO.NET Ent

2009-12-30 14:03:36

ADO.NET Ent

2009-12-30 10:14:41

ADO.NET Ent

2009-12-22 16:03:34

ADO.NET Ent

2009-12-22 17:14:37

ADO.NET Ent

2009-12-30 09:10:04

ADO.NET Ent

2009-12-23 16:00:50

ADO.NET Ent

2009-12-30 10:43:31

ADO.NET Ent

2009-12-23 16:15:24

ADO.NET Ent

2009-09-04 14:52:03

ADO.NET Ent

2009-12-31 14:56:36

ADO.NET Ent

2009-11-11 15:59:17

ADO.NET Ent

2009-12-28 15:11:36

ADO.NET专家

2011-05-20 17:05:59

ADO.NET

2009-12-30 10:02:37

ADO.NET Ent

2009-12-30 10:49:32

ADO.NET Ent

2009-12-22 17:09:34

ADO.NET Ent

2009-11-12 13:26:56

使用ADO.NET参数

2009-12-23 17:50:38

ADO.NET Fra
点赞
收藏

51CTO技术栈公众号