views:

125

answers:

2

Hi,

Is there anyway to map int field into enum in EFv1? Thanks! I want my entity to have enum field rather than int field.

A: 

You can simply cast the int to the Enum like this:

public enum TestEnum
{
Zero = 0,
One,
Two
}

TestEnum target = (TestEnum)1;

Target should then contain TestEnum.One;

Edit: My bad, did not interpret properly at first. You want the map to handle the cast for you, right? Don't know that right now, would have to experiment a bit.

Anton
+2  A: 

Create two properties. One mapped to EF, one as a wrapper

[EdmScalarProperty]
public int EnumPropInteger {get;set}
public MyEnum EnumProp
{
    get { return (MyEnum) EnumPropInteger; }
    set { EnumPropInteger = (int)value; }
}

Not a nice way because you have two public properties but a way.

Arthur
You can set the int property to be private, internal, or protected.
devlife
And EF can access then this Property?
Arthur