Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums in EF4 POCO Generated Objects

Since EF4 is lacking enum support, I've been trying to implement the workaround listed at:

http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx?PageIndex=1&CommentPosted=true#comments

However, I'm using the POCO generator for EF4 (which the article does NOT use) and I keep getting the following runtime error:

Mapping and metadata information could not be found for EntityType...

Presumably this is because CreateObjectSet doesn't understand the wrapper class.

Has anyone been able to find a suitable solution for supporting enums in EF4 with generated POCOs?

Thanks.

like image 308
goombaloon Avatar asked Feb 28 '11 21:02

goombaloon


1 Answers

Yes, enum type properties are not supported by EF4 (or CTP5); of course we need them, and I've heard that they will be implemented next release.

Here is a workaround:

public enum FieldDataType
{ 
    Image,
    RawText,
    Ajax
}

public class DefinitionDynamicField
{
    public int FieldType { get; set; }

    [NotMapped]
    public FieldDataType FieldTypeObserver 
    { 
        get { return (FieldDataType)FieldType; }
        set { return FieldType = (int)value; }
    }
}

We use FieldTypeObserver instead of FieldType.

It is ugly but it works.

like image 147
Nuri YILMAZ Avatar answered Sep 21 '22 14:09

Nuri YILMAZ