Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map lookup table to enum?

Suppose i have the following 2 SQL tables:

Foo

Column        DataType
---------------------------
Title         NVARCHAR(20)
Body          NVARCHAR(MAX)
FooTypeId     TINYINT

FooType

Column        DataType
--------------------------
FooTypeId     TINYINT
Name          NVARCHAR(10)

Now, im using Entity Framework 4.0 with a custom data context and POCO implementation.

How do i map this on the designer, and my POCO's?

Do i have to create a POCO property (of type byte i assume) called "FooTypeId", then i expose ANOTHER property of my enum type?

Ie.

public class Foo
{
    public byte FooTypeId { get; set; } // for ORM - do i need this??
    public FooType FooType // for most querying operations
    {
         get
         {
            return (FooType)this.FooTypeId;
         }
         set
         {
            this.FooTypeId = (int)value;
         }
    }
}

public enum FooType
{
    Blah = 1,
    Foo = 2,
    Bar = 3
}

At the moment, i do not even have the FooType table on my designer, as i figured i can try and "express" this as an enumeration from the actual FooTypeId on the Foo property. Or am i supposed to create a "Navigational Property" on the mapper, then define that in my POCO?

I've read threads from a few years back (EF1) saying "Enums are not supported in EF", is this still the case with EF4? If it is, is what im doing right?

I'm kind of lost here, some guidance would be greatly appreciated!

like image 545
RPM1984 Avatar asked Sep 06 '10 04:09

RPM1984


1 Answers

Currently, EF4 doesn't support enums natively.

I've seen a great workaround by AlexJ that works pretty well (it's pretty code heavy though), http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx

I've also heard that this native enum support is coming in the next version of EF4, but who knows exactly when that will be released.

like image 183
Alastair Pitts Avatar answered Oct 15 '22 22:10

Alastair Pitts