Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104811

Enum typed properties in Entity Framework entities?

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.

Upvotes: 3

Views: 627

Answers (3)

Thomas Levesque
Thomas Levesque

Reputation: 292685

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...

Upvotes: 4

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104811

Enums are supported on EF ≥ 5.

See this.

Upvotes: 0

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104811

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

Upvotes: 2

Related Questions