Use of enumerations in the Ado.net Entity Framework 4

Source: Internet
Author: User

This article will lead us into the ado.net world through the use of the enumeration in the Ado.net Entity Framework 4.

An enumeration (enum) is a commonly used type, such as a parameter to represent a state, a type, and so on. But at the moment it will not be officially supported in the Ado.net Entity framework. This article describes the use of enumerations through complex types (Complex Types) in Ado.net Entity Framework 4.

This method requires the use of the Poco class, not the classes that are automatically generated by Visual Studio. Because we need to write code for complex types manually.

Database scripts:

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 is not NULL,

Password varchar () NOT NULL,

Email nvarchar (MB) NOT NULL,

role int is 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 that holds the account information, and role is an enumeration type, in the database with the int type.

We write an enumeration type to represent role in the usual way

public enum Accountroleenum {

Admin = 1,

User = 2

}

A complex type is then written to transform between the enumeration type and the int type of the database. Complex types are only available in the 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 to role. Enumvalue;

}

The last 2 methods are used for implicit type overloading, which is to transform the type.

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 the Entity 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 is done, and in comparison you can use

Account. role = = Accountroleenum.admin But when it comes to querying the database, such writing is an error, and can only be used

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.