Roy
Roy

Reputation: 2323

How to map int into enum with EF

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

Upvotes: 5

Views: 4153

Answers (3)

VinnyG
VinnyG

Reputation: 6911

It's supported with the new release : Now supported : http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx

Upvotes: 2

Anton
Anton

Reputation: 5623

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.

Upvotes: -3

Arthur
Arthur

Reputation: 8129

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.

Upvotes: 7

Related Questions