ADO.NET Entity Framework 4中枚舉的使用
來(lái)源:易賢網(wǎng) 閱讀:800 次 日期:2014-08-20 13:58:04
溫馨提示:易賢網(wǎng)小編為您整理了“ADO.NET Entity Framework 4中枚舉的使用”,方便廣大網(wǎng)友查閱!

本文將通過(guò)ADO.NET Entity Framework 4中枚舉的使用介紹,帶領(lǐng)大家走進(jìn)ADO.NET的世界。

枚舉(Enum)是一種常用的類(lèi)型,如用于表示狀態(tài)、類(lèi)型等參數(shù)。但目前它不會(huì)被官方地在ADO.NET Entity Framework中進(jìn)行支持。本文介紹的是通過(guò)復(fù)雜類(lèi)型(Complex Types)在ADO.NET Entity Framework 4中使用枚舉。

這種方法需要使用POCO類(lèi),而不能使用Visual Studio自動(dòng)生成的類(lèi)。因?yàn)槲覀冃枰謩?dòng)為復(fù)雜類(lèi)型編寫(xiě)代碼。

數(shù)據(jù)庫(kù)腳本:

if exists (select 1

from sysobjects

where id = object_id('Account')

and type = 'U')

drop table Account go create table Account

(

ID uniqueidentifier not null default NewSequentialID(),

UserName nvarchar(20) not null,

Password varchar(40) not null,

Email nvarchar(100) not null,

Role int not null,

constraint PK_ACCOUNT primary key (ID)

)

insert into Account (UserName ,Password,Email ,Role ) values ('Test1','Test1','test1',1)

insert into Account (UserName ,Password,Email ,Role ) values ('Test2','Test2','test2',1)

insert into Account (UserName ,Password,Email ,Role ) values ('Test3','Test3','test3',2)

這是一個(gè)用于存放帳號(hào)信息的數(shù)據(jù)表,Role是個(gè)枚舉類(lèi)型,在數(shù)據(jù)庫(kù)中用int類(lèi)型。

我們按常規(guī)做法寫(xiě)一個(gè)用于表示Role的枚舉類(lèi)型

public enum AccountRoleEnum {

Admin = 1,

User = 2

}

然后寫(xiě)一個(gè)復(fù)雜類(lèi)型用于在枚舉類(lèi)型和數(shù)據(jù)庫(kù)的int類(lèi)型之間做變換。復(fù)雜類(lèi)型只有在ADO.NET Entity Framework 4中才有。

public partial class RoleWrapper

{

private AccountRoleEnum m_orderStatus;

public int Value

{

get {

return (int)m_orderStatus;

}

set {

m_orderStatus = (AccountRoleEnum)value;

} }

public AccountRoleEnum EnumValue

{

get {

return m_orderStatus;

}

set {

m_orderStatus = value;

}

}

public static implicit operator RoleWrapper(AccountRoleEnum role)

{

return new RoleWrapper {

EnumValue = role

};

}

public static implicit operator AccountRoleEnum(RoleWrapper role)

{

if (role == null)

return AccountRoleEnum.User;

return role.EnumValue;

}

} 最后的2個(gè)方法用于隱式類(lèi)型重載,也就是對(duì)類(lèi)型進(jìn)行變換。

然后我們寫(xiě)Account實(shí)體。

public class Account

{

public Guid ID

{

get;

set;

}

public string UserName { get; set;

}

public string Password

{

get;

set;

}

public string Email

{

get;

set;

}

public RoleWrapper Role

{

get;

set;

} 和實(shí)體框架上下文。

public class EntitiesContext : ObjectContext

{

public EntitiesContext()

: base("name=Entities", "Entities")

{

_accounts = CreateObjectSet();

}

public ObjectSet Accounts

{

get

{

return _accounts;

}

}

private ObjectSet _accounts;

}

這樣,主要的工作就已經(jīng)完成了,在比較時(shí)可以使用

account.Role == AccountRoleEnum.Admin 但是在涉及到數(shù)據(jù)庫(kù)的查詢(xún)時(shí),這樣的寫(xiě)法是會(huì)報(bào)錯(cuò)的,只能使用

EntitiesContext db = new EntitiesContext(); db.Accounts.Where(c => c.Role.Value == (int)AccountRoleEnum.Admin);

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:ADO.NET Entity Framework 4中枚舉的使用
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)