5 Temmuz 2012 Perşembe

enum support in entity framework

Enumeration support comes with Entity Framework 5, so for prior versions we need a workaround..
I think the workaround in http://dotnetdevdude.com/Blog/2012/01/09/EntityFrameworkCodeFirstEnum.aspx is suitable.  To apply the workaround:
1.   Create two members for the field you want to use as enum type. In the below example,   SentencePart   is an enumaration which specifies parts of a sentence. SentencePartInt  holds an integer value whereas SentencePartEnum  is the member which is going to be used and processed in the code.
        public Nullable SentencePartInt{ get; set; }
        public Nullable< SentencePart> SentencePartEnum
            get { return ( SentencePart) SentencePartInt ; }
            set {  SentencePartInt = (int)value; }

2.   Tell the database context not  to persist the enum member.
       modelBuilder.Entity().Ignore(x => x. SentencePartEnum );
3.    Use the enum member in the code, not the integer.

1 yorum:

  1. Hilal

    This is even better than Entity Framework: