首页 / 软件开发 / .NET编程技术 / 在ADO.NET Entity Framework 4中使用枚举
在ADO.NET Entity Framework 4中使用枚举2011-08-29 博客园 Snowdream枚举(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
5 }