Code first – Enums Supported in EF June 2011 CTP

Support for enumeration has been one of the long desired requests in Entity Framework. As soon as I saw the June 2011 CTP, I had to test it. Here is what I did

1. I created windows project TestEfJuneCtp

image

2. I changed the Target framework to “Microsoft Entity Framework June 2011 CTP”

image

3.    Created a couple of classes project, order and an enumeration ProjectStatusType

 

image

4.    Project has a property projectStatus of type ProjectStatusType, this would have been ignored in EF 4.1. meaning when you generate the database projectStatus column would not appear in the project table. Now in the current CTP it does appear of type Int.

5.    Then I set up my Context, I did add reference to system.data.entity v4.2

image

6.    Using Enums to set values

image

7.    If you want to set value other than the one in Enum then just Directcast
image
8.    Since I set projectStatus to InProgress, it is set to 0 in the database column
9.    Get Values from Enum properties

image

When you do projectEntity.projectStatus it returns “0”, but when you do do projectEntity.projectStatus.ToString it returns “InProgress”

10.    Also the power of enumeration can be more profound when writing code sinec intellisence can pick up the type

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s