views:

71

answers:

2

Is there a way I can map generated entities to enum?

And what I mean is simple:

class Person
{
    RelationshipStaus RelationshipStatus { get; set; }
}

enum RelationshipStatus : byte
{
    Single,
    Married,
    Divorced
}

The property RelationshipStatus in the DB is a simple byte, I want that in my project it should be an enum.

+2  A: 

Unfortunately, you can't, at least not directly. For convenience, you can create an accessor that converts the value to and from the enum type:

public int RelationshipStatusInt { get; set; }

public RelationshipStatus RelationshipStatus
{
    get { return (RelationshipStatus)RelationshipStatusInt; }
    set { RelationshipStatusInt = (int)value; }
}

However you won't be able to use that properties in Linq to EF queries, because it won't be mapped to a DB column (but you can use it in Linq to Objects queries).

Another solution is described here, but it feels a bit awkward...

Thomas Levesque
+1  A: 

As Thomas said, there is no solution.

I would just recommend users who encountered the desire to use enums in EF, and vote for this connection: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015335-support-for-enums

Shimmy
I would love to, but the link you posted isn't working for me.
StriplingWarrior
@StriplingWarrior Link updated.
Shimmy