Use of enumeration in ADO. NET Entity Framework 4

Source: Internet
Author: User

Use of enumeration in ADO. NET Entity Framework 4

This article will introduce how to use enumeration in ADO. NET Entity Framework 4 to bring everyone into the world of ADO. NET.

Enum is a common type, for example, used to indicate parameters such as status and type. However, it is not officially supported in ADO. NET Entity Framework. This article describes how to use enumeration in ADO. NET Entity Framework 4 by using Complex Types.

This method requires the POCO class instead of the class automatically generated by Visual Studio. Because we need to manually write code for complex types.

Database script:

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)

This is a data table used to store account information. Role is an enumeration type and int type is used in the database.

We write an enumeration type to indicate the Role according to the general practice.

Public enum AccountRoleEnum {

Admin = 1,

User = 2

}

Then, write a complex type for transformation between the enumerated type and the int type of the database. Complex types are available only in 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;

}

} The last two methods are used for implicit type overloading, that is, type conversion.

Then we write the Account entity.

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;

} And object framework context.

Public class EntitiesContext: ObjectContext

{

Public EntitiesContext ()

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

{

_ Accounts = CreateObjectSet ();

}

Public ObjectSet Accounts

{

Get

{

Return _ accounts;

}

}

Private ObjectSet _ accounts;

}

In this way, the main work has been completed and can be used for comparison.

Account. Role = AccountRoleEnum. Admin

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.