Using enumerations in the Ado.net Entity Framework 4

Source: Internet
Author: User
Tags visual studio

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:

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)

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

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

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.